Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

so I have this custom directives that you could see below.

myApp.directive('myDirective', function () {
    return {
        restrict:'EA',
        link:function (scope, element, attr) {
           //defined the object
           var object = new object();
    }
   }
});

myApp.directive('mySecondDirective', function () {
    return {
        restrict:'EA',
        link:function (scope, element, attr) {
           //call the variable from previous custom directive
           console.log(object);
    }
   }
});

and this is the html structure where I used the directives above.

<my-directive></my-directive>

<my-second-directive></my-second-directive>

there I want to retreive the object that contains new object() from previous custom directive, but it always return an undefined I wonder how could I do this without using require nor isolated scope as well.. could you guys help me ?

share|improve this question
    
Try using scope.object = new object(); in the first directive. Access the same in second directive using scope.object –  Srinivas Paila 9 hours ago
    
already tried it but still returned with undefined response :( @SrinivasPaila –  user3860691 48 mins ago

1 Answer 1

1). Scope. Since you don't want to use controllers and isolated scope, then you can simply set this object as scope property.

myApp.directive('myDirective', function() {
    return {
        restrict: 'EA',
        link: function(scope, element, attr) {

            var object = {};
            object.test = 21;

            // set object as scope property
            scope.object = object;
        }
    }
});

myApp.directive('mySecondDirective', function() {
    return {
        priority: 1,  // priority is important here
        restrict: 'EA',
        link: function(scope, element, attr) {
            console.log('Retrieve: ', scope.object);
        }
    }
});

Just make sure you are also defining priority on the second directive (only if both directive a applied to the same element) to make sure it's evaluated in proper turn (should be bigger then the one of myDirective, if omitted it's 0).

2). Service. Another simple solution is to use service. You can inject custom service into both directives and use it storage for you shared object.

share|improve this answer
    
yap already sets the priority as well but still returned with undefined response :( @dfsq –  user3860691 47 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.