Passing variable to Angular Directive

61,317

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

Share:
61,317
Massive Boisson
Author by

Massive Boisson

Updated on July 09, 2022

Comments

  • Massive Boisson
    Massive Boisson almost 2 years

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