Why are my AngularJS directives sharing scope?

nickponline

I've tried to make a simple directive which displays a name and allows it to be change. When I put multiple directive on the name page they all seem to share the name attribute. What am I doing wrong?

<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset=utf-8 />
<title></title>

  <script src="http://code.angularjs.org/1.2.0-rc.3/angular.min.js"></script>
  <script src="http://code.angularjs.org/1.2.0-rc.3/angular-resource.min.js"></script>
  <script src="http://code.angularjs.org/1.2.0-rc.3/angular-animate.min.js"></script>
  <script>
    var app = angular.module('app', []);

    app.directive('person', function () {

    function link ($scope, elem, attrs, ctrl) {     

        $scope.name = "OLD"        

        $scope.setName = function() {
            $scope.name = 'NEW';
        }
    }

    return {
      restrict: 'E',
      replace: true,
      template: "<span>Current name = {{name}}<a href='' class='btn' ng-click='setName()'>Change name</a><br></span>",
      link : link,
    }

  });

  app.controller('MainCtrl', function ($scope) { });

  </script>    

</head>

<body ng-controller='MainCtrl'>
  <person></person><br>
  <person></person><br>
  <person></person><br>
  <person></person><br>
</body>

</html>
Stokes Player

As mentioned in previous answers, the default behavior of AngularJS directives is to share the scope that they are included in. This behavior is changed via the scope parameter in the directive definition object.

You can view the documentation for the scope argument in this section of the AngularJS documents: http://docs.angularjs.org/api/ng.$compile#description_comprehensive-directive-api_directive-definition-object

This argument has three options:

  1. scope: false - the default behavior of sharing the scope the directive is included in

  2. scope: true - create a new scope for the directive that acts like other child scopes and prototypically inherits from its parent scope

  3. scope: {} - create an isolated scope that does not prototypically inherit from its parent scope

As you can see with the JSBin examples, both options 2 and 3 will work for your example. The difference is whether you want your new scopes isolated or not.

The directives section of the AngularJS guide has a good section on why isolated scope can help create better reusable modules with directives: AngularJS Guide: Isolating the Scope of a Directive

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AngularJS : directives and scope

From Dev

AngularJs scope reference with directives

From Dev

Why is my AngularJS $scope not working?

From Dev

AngularJS Modules/Scope Sharing

From Dev

angularjs inheriting scope in nested directives

From Dev

Isolate scope in Angularjs custom directives - "="

From Dev

AngularJs attributes vs scope in directives

From Dev

AngularJS : Nested Directives and Scope Inheritance

From Dev

AngularJs attributes vs scope in directives

From Dev

Does anyone know why my AngularJS directives do not display as expected?

From Dev

AngularJS directives - Isolated Scope and Inherited Scope

From Dev

AngularJs: sharing model data between directives

From Dev

Issue in scope sharing with angularjs Modal

From Dev

AngularJS: Sharing scope behavior with sibling or descendant scope

From Dev

angularjs - isolate scope between multiple different directives

From Dev

AngularJS Directives - Can Isolated Scope Attributes be required?

From Dev

AngularJs: multiple directives asking for isolated scope on

From Dev

AngularJS directives - isolated scope special binding characters

From Dev

Angularjs isolated scope for directives without own template

From Dev

Using "this" instead of "scope" with AngularJS controllers and directives

From Dev

angularjs - Multiple directives asking for new / isolated scope

From Dev

AngularJS directive - template from $scope with other directives

From Dev

AngularJS directives - isolated scope special binding characters

From Dev

AngularJS Directives - Can Isolated Scope Attributes be required?

From Dev

AngularJS : Scope is not shared between nested transcluded directives

From Dev

angularjs - isolate scope between multiple different directives

From Dev

AngularJS directives: Why watcher is not triggered?

From Dev

Why won't $scope be found in my AngularJS / Karma unit test?

From Dev

AngularJS - Why is my directive isolated scope variable is undefined?

Related Related

  1. 1

    AngularJS : directives and scope

  2. 2

    AngularJs scope reference with directives

  3. 3

    Why is my AngularJS $scope not working?

  4. 4

    AngularJS Modules/Scope Sharing

  5. 5

    angularjs inheriting scope in nested directives

  6. 6

    Isolate scope in Angularjs custom directives - "="

  7. 7

    AngularJs attributes vs scope in directives

  8. 8

    AngularJS : Nested Directives and Scope Inheritance

  9. 9

    AngularJs attributes vs scope in directives

  10. 10

    Does anyone know why my AngularJS directives do not display as expected?

  11. 11

    AngularJS directives - Isolated Scope and Inherited Scope

  12. 12

    AngularJs: sharing model data between directives

  13. 13

    Issue in scope sharing with angularjs Modal

  14. 14

    AngularJS: Sharing scope behavior with sibling or descendant scope

  15. 15

    angularjs - isolate scope between multiple different directives

  16. 16

    AngularJS Directives - Can Isolated Scope Attributes be required?

  17. 17

    AngularJs: multiple directives asking for isolated scope on

  18. 18

    AngularJS directives - isolated scope special binding characters

  19. 19

    Angularjs isolated scope for directives without own template

  20. 20

    Using "this" instead of "scope" with AngularJS controllers and directives

  21. 21

    angularjs - Multiple directives asking for new / isolated scope

  22. 22

    AngularJS directive - template from $scope with other directives

  23. 23

    AngularJS directives - isolated scope special binding characters

  24. 24

    AngularJS Directives - Can Isolated Scope Attributes be required?

  25. 25

    AngularJS : Scope is not shared between nested transcluded directives

  26. 26

    angularjs - isolate scope between multiple different directives

  27. 27

    AngularJS directives: Why watcher is not triggered?

  28. 28

    Why won't $scope be found in my AngularJS / Karma unit test?

  29. 29

    AngularJS - Why is my directive isolated scope variable is undefined?

HotTag

Archive