How do you read the value assigned to an AngularJS directive

angularjs, javascript

Solution

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);
       }
   }
});

Problem

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?

Original source