4

For example I have directive

App.directive('module', function($compile)
{   
    return  {

        replace     : true,         
        restrict    : 'E',
        link: function(scope, iElement, iAttrs)
        {   
            scope.localName = '1';
        },

        template : '<div> {{ name }} - {{ localName }}</div>',  
    }       
});

At application run function:

App.run(function($rootScope, $location)
{
    $rootScope.name = "test";
}

So in this way directive's scope will be same for all directives but this scope will have access to $rootScope:

<module></module> 
<module></module>

https://i.sstatic.net/QF8wM.png

But if I will make an isolated scope:

App.directive('module', function()
{   
    return  {

        replace     : true,         
        restrict    : 'E',
        link: function(scope, iElement, iAttrs)
        {   
            scope.localName = '1';
        },

        template : '<div> {{ name }} - {{ localName }}</div>',
            scope : {}  
    }       
});

Scopes will be different, but they will be have access to $rootScope.

https://i.sstatic.net/XITHr.png

So I need to isolate the scope of each directive but I need this scope to have access to $rootScope.

Can you help me please?

0

1 Answer 1

4

scope: true

not isolated, not shared, but a new prototipically inherited scope per directive instance.

http://jsfiddle.net/4LAjf/1

You can log the id of the scope with console.log(scope.$id). With scope: {} you'll get, for example, 003 and 004. With scope: false (or nothing), the scope is shared, e.g. both have 002. With scope:true they have different id's but can read $rootScope.

In the live example you can see the scope ID in your local variable localName. I named the dummy directive mydir instead of module. It just looks weird naming module a directive.

More about AngularJS scopes

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

1 Comment

Save my life did not know this thing could cause so much problems

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.