0

I have 2 directives on the same tag, <settings> and <modal>. settings provides a template, while modal creates an isolated scope:

jsfiddle

var app = angular.module('app', []);

app.directive('settings', function () {
    return {
        restrict: 'E',

        template: '<div>SETTINGS</div>',

        link: function (scope) {
            console.log('settings', scope.$id);
        }
    };
});

app.directive('modal', function () {
    return {
        restrict: 'A',

        scope: {},

        link: function (scope) {
            console.log('modal', scope.$id);
        }
    };
});

However, they do not end up sharing the same scope, as shown by the logs:

<settings modal="settings"></settings>

settings 002
modal 003 

Why is that ?

3 Answers 3

1

Till version 1.2.0rc3, sibling directives were sharing the same isolated scope if created.

As of version 1.2.0, you cannot (and shouldn't) access the isolated scope from outside the contents compiled against this isolated scope. You shouldn't because you are creating a hidden relation between both directives. Better use directive's controller requirement via the require property, or share informations through injected services if singleton pattern can be applied to your use case.

It is finally deeply related to this other question about Directives with Isolated scope versions conflict

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

Comments

1

scope: {} option always creates an isolated scope for the directive, it doesn't care about other directives

3 Comments

OK, but can I somehow get the 2 directives to share the same (isolated) scope ? They can't both create an isolated scope.
you can make the non-isolated directive ember into the isolated one, or create an isolated wrapper directive and then keep both of them non-isolated... I don't think it's a good idea to mix non-isolated/isolated directives as one element
I was trying to avoid that, possibly by forcing the isolating directive to transclude the non-isolated one, but I guess I have no choice…
0
app.directive('modal', function () {
    return {
        restrict: 'A',

        scope: false,

        link: function (scope) {
            console.log('modal', scope.$id);
        }
    };
});

scope: false will not create a new isolated scope. I you want to make two directives work between them use require a controller on the directive that way you can share data between them.

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.