Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 443
Massive Boisson Avatar asked May 08 '13 03:05

Massive Boisson


People also ask

How do you pass data into a directive?

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.

What is attribute directive in Angular?

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.

What are types of directives in Angular?

The three types of directives in Angular are attribute directives, structural directives, and components.


1 Answers

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

like image 192
Arun P Johny Avatar answered Sep 24 '22 07:09

Arun P Johny