Massive Boisson
Massive Boisson

Reputation: 1627

Passing variable to Angular Directive

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".

Upvotes: 42

Views: 62139

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388436

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

Upvotes: 71

Related Questions