Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Two matching directives don't see the same scope #8143

Closed
Izhaki opened this issue Jul 11, 2014 · 1 comment
Closed

Two matching directives don't see the same scope #8143

Izhaki opened this issue Jul 11, 2014 · 1 comment

Comments

@Izhaki
Copy link
Contributor

Izhaki commented Jul 11, 2014

If one defines two directives with isolated scope that match the same element (plunk), one gets:

Error: [$compile:multidir] Multiple directives [directive1, directive2] asking for new/isolated scope on: <div directive1="" directive2="">

One can infer that scopes are attached to DOM nodes, not to the directives themselves.

(It has to be mentioned that while two matching directives cannot declare an isolated scope, they are both free to create one 'manually' in the controller/link functions - essentially allowing each to have an isolated scope.)

But if only one directive has an isolated scope, the other directive doesn't see it. As in this example (see plunk):

index.html:

<html ng-app='myApp'>

<head>
    <meta charset='utf-8'>
    <title>AngularJS IsoScope</title>
    <script src='//ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular.js'></script>
    <script src='index.js'></script>
</head>

<body>
    <div directive1 directive2></div>
</body>

</html>

index.js:

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

myApp.directive( 'directive1', function( $compile, $interpolate ) {
    return {
        scope: {},
        controller: function( $scope, $element, $attrs, $transclude ) {
            console.log( $scope.$id );
        },
    }
});

myApp.directive( 'directive2', function( $compile, $interpolate ) {
    return {
        controller: function( $scope, $element, $attrs, $transclude ) {
            console.log( $scope.$id );
        },
    }
});

This outputs:

003
002

So what scopes are attached to exactly? If it's the DOM node, both directive should see the same scope; if it's the directive, why two directives can't define their own isolated scope?

@btford
Copy link
Contributor

btford commented Jul 21, 2014

Please read the contributing guidelines; these types of questions are ill-suited for GitHub. Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants