If I have a directive myDir
and I call it within ng-repeat
like so
<my-dir myindex="{{$index}}"></my-dir>
How can I access myindex
? I get actual string {{$index}}
when I use attrs.myindex
within postLink
function. When I inspect html, it actually says myindex="2"
.
If you want to send data to directive then you should try like this: This is my custom directive, and I am going to share two value from component or HTML to the directive. You will have to use your directive in your html and send data to the directive like in below code. I am sending name and value to the test.
The attribute directive changes the appearance or behavior of a DOM element. These directives look like regular HTML attributes in templates. The ngModel directive which is used for two-way is an example of an attribute directive.
The three types of directives in Angular are attribute directives, structural directives, and components.
Try
<my-dir myindex="$index"></my-dir>
Then
app.directive('myDir', function () { return { restrict: 'E', scope: { myindex: '=' }, template:'<div>{{myindex}}</div>', link: function(scope, element, attrs){ scope.myindex = attrs.myindex; console.log('test', scope.myindex) } }; })
Demo: Plunker
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With