use scope value as attribute of element in directive

25 views
Skip to first unread message

Fabricio Coutinho

unread,
Apr 16, 2015, 10:38:27 PM4/16/15
to ang...@googlegroups.com
Hi,

I would like my directive use scope params as attributes not as value of attribute in element. (sorry for my bad english)

app.angular.directive('myInput', myInput);

  function myInput(){
    var directive = {
      restrict: 'E',
      replace: true,
      scope: {
        name: '@',
        masktype: '@',
      },
      template: '<input  type="text" id="{{name}}"  masktype>'

HTML

 <my-input type="text" name="myinput1" masktype="custom-attr" ></my-input>

Render this
<input type="text" id="myinput1" custom-attr></input>

Sander Elias

unread,
Apr 17, 2015, 4:41:37 AM4/17/15
to ang...@googlegroups.com
Hi Fabricio,

You can't do that using templates. It can be done during compile time, or during link time. Link is the easiest, but if those 'custom-attr' modify angular behavior, you need to do it during compile.

Regards
Sander

Fabricio Coutinho

unread,
Apr 18, 2015, 1:23:03 PM4/18/15
to ang...@googlegroups.com
Thanks Sander,

Its works, but I had to search the element to add the attribute needed.
Ex.
<div>
   <label>some value</label>
   <input custom-attr>
</div>
Reply all
Reply to author
Forward
0 new messages