1

I have created two directives and inserted the first directive into the second one. The content of template attribute works fine but the scope variable of the controller is not recognized. Please provide me solution on this

sample link: http://jsbin.com/zugeginihe/2/

2 Answers 2

1

You didn't provide the attribute for the second directive.

HTML

<div second-dir first-dir-scope="content">

  <div first-dir first-dir-scope="content"></div>

</div>

Link demo: http://jsbin.com/jotagiwolu/2/edit

Sign up to request clarification or add additional context in comments.

4 Comments

thanks a lot. is there any other way which can be done in the js file rather than view
You can get the content value by using "console.log(scope.$parent.content);". But it is a bad idea!
shall we use require: "^firstDir" in the second directive?
@user1153484 Yes you can do this, and it would be better way
1

The best option would using parent directive, We could take use of require option of directive like require: '?secondDir' in firstDir

Code

var myApp = angular.module("myApp", []);

myApp.controller("myController", function($scope) {
    $scope.content = "test1";
});

myApp.directive("firstDir", function() {
    return {
        restrict: "AE",
        require: '?secondDir',
        scope: {
            firstDirScope: "="
        },
        template: "<div>first content</div>",
        link: function(scope, element, attrs, secondDir) {
            console.log(scope.firstDirScope, 'first');
        }
    };
});

myApp.directive("secondDir", function() {
    return {
        restrict: "AE",
        scope: {
            firstDirScope: "="
        },
        controller: function($scope) {
            console.log($scope.firstDirScope, 'second');
        }
    };
});

Working JSBin

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.