Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you read the value assigned to an AngularJS directive

I have an angular directive that is a custom attribute that possibly can contain a value like so:

<div my-directive="myVal"></div>

How do I read myVal (as a string) from inside my directive's link function?

like image 793
Dustin Avatar asked Oct 31 '12 14:10

Dustin


Video Answer


1 Answers

The passed value is on the attributes object passed as the third argument to the link function. It's under a property matching the name of the directive.

app.directive('myDirective', function() {
   return {
       restrict: 'A',
       link: function(scope, elem, attr) {
            //read the passed value
            alert(attr.myDirective);
       }
   }
});
like image 179
Ben Lesh Avatar answered Nov 05 '22 11:11

Ben Lesh