0

Scenario

Lets say I have two directives first and second where the directives are used on the same DOM element as such:

HTML

<div first second></div>

Javascript

//first directive
.directive('first', function(){
return {
    restrict: 'A',
    priority: 1,
    link: function(scope, element, attrs){
        element.attr('someAttr', 1234);
        element.html('I have the attribute value: ' + element.attr('someAttr'));
       }
   };
})
// second directive
.directive('second', function(){
return {
    restrict: 'A',
    priority: 0,
    controller: function($scope, $element, $attrs){
        // the elements attribute `someAttr` is undefined here
    }
   };   
})

The first directive adds an attribute (someAttr) to the element where it is being used.

Problem

What I want to achieve is that access/use the set value to someAttr (set in first directive) in the second directive's link/controller function. But for the moment, I haven't been successful in doing so. (Check this fiddle out)

Note: I've also set the priority of the first directive to a value higher than that of second, but still when you log the attributes of the element where the directive is used, there is no someAttr attribute in the set.

Also note that if how I want to achieve the communication between directives is not appropriate, then what is the right way to do so? Any help would be appreciated.

2
  • 1
    what is the practical problem you are trying to solve here? there are much better ways to enable directives to communicate with each other.... Commented Nov 10, 2015 at 9:02
  • The real reason I want to access the attribute between those directives is that, in fact, the first directive (in my own case uid directive) generates a unique id for the DOM element. Whereas the the second directive (storeJSON directive) is responsible for creating/storing a DOM JS object based on the unique ID set in the first directive, as well as placing this object in the appropriate position, in a hierarchy of DOM JS objects. Commented Nov 10, 2015 at 9:09

1 Answer 1

1

You cannot cannot detect changes in attributes set to the DOM element directly (at least not in angularjs way).

Try using a property in the first directive controller and then 'require' 'first' in 'second' to access 'second' properties.

Check the information about the 'requires' property when defining a directive to see how it works.

UPDATE: Check this plunkr:

http://plnkr.co/edit/bEbO8LvCIUd0NZPFqMnv?p=preview

app.directive('first', function(){
return {
    restrict: 'A',
    link: function(scope, element, attrs){
        element.html('I (first) have the scope value: ' + scope.data.attribute);
       },
       controller: function($scope) {
         this.data = $scope.data = {
           attribute: 1234
         };
       }
   };
});
// second directive
app.directive('second', function(){
return {
    restrict: 'A',
    require: 'first',
    priority: 1,
    link: function($scope, element, attrs, firstController){
        element.html(element.html() + ' and I (second) have access to that value too: ' + firstController.data.attribute);
        //you can $scope.$watch firstController.data changes here too
    }
   };   
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.