How to change all directive scope value from any one of directive?

VijayVishnu

I have a directive which was repeatedly n times. I have click event action in every directive. Whenever I click, on click function, It change some scope variable names which defined in directive. But It wont change on all other directives? How to achieve this? I have my plunker here.example

html:

  <!doctype html>
<html ng-app="myApp" >
<head>
  <meta charset="utf-8">
  <title>AngularJS Plunker</title>
  <script>document.write('<base href="' + document.location + '" />');</script>
  <link rel="stylesheet" href="style.css">
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.js"></script>
  <script src="script.js"></script>
</head>
<body ng-controller="MainCtrl">
  this is 1st time: <div sampledirective></div>
  <br />
  this is 2nd time: <div sampledirective></div>
  <br />
  this is 3th time: <div sampledirective></div>
  <br />
  this is 4th time: <div sampledirective></div>
  <br />
  this is 5th time: <div sampledirective></div>
</body>
</html>

I have rendered 'sampledirective' as 5 times in my view.

controller:

  app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
  $scope.bar ="original bar";
  $scope.term = "original term"
  $scope.temp={};
  $scope.callMethod = function (){
    $scope.term = "Changed term"
    $scope.bar = "Changed bar";
    $scope.temp.callMethod();
  }

});

I have created one object 'temp' in my controller.

directive:

app.directive('sampledirective', function($http) {
  return {
      restrict: 'A',
      scope:true,
       replace: true,
       link:function(s,e,a){
         s.directive_scope="original directive_scope value";
         s.temp.callMethod = function(){

            s.directive_scope="Changed directive_scope value";
         }

       },
       template: '<div ><a ng-click="callMethod()">click_here</a> <span> <b>{{bar}}</b></span>   <span> <b>{{term}}</b></span>  <span> <b>{{directive_scope}}</b></span></div>'
  }

});

In my directive template I have 'temp.callMethod()' which is changed the scope variable value 'directive_scope= Changed directive_scope value'. But It was not reflected in other directives. How to achieve this?

Dennis Baizulin

Problem

Take a closer look at your directive definition, notice you put scope:true in there.

What it does is it tells Angular to create a new isolated scope for this particular instance of the directive inheriting it from the parent one.

So you are stuck with 5 directive instances all with their own scopes and when you assign directive_scope in one of them it will in no way reflect in the others. You can read more about directives and their scopes here

Solution 1

You should define property in the parent scope and then pass it to the directive using two-way binding

Controller definition

app.controller('MainCtrl', ['$scope', function ($scope) {
    $scope.directiveScope = null;
    $scope.temp = {};
    $scope.callMethod = function () {
        $scope.term = "Changed term"
        $scope.bar = "Changed bar";
        $scope.temp.callMethod();
    };
}]);

Directive definition

app.directive('sampleDirective', function () {
    return {
        ...
        scope: {
            temp: '=',
            callMethod: '&',
            directiveScope: '=' 
        },
        ...
    };
});

HTML

<div sample-directive temp="temp" call-method="callMethod()" directive-scope="directiveScope"></div>

Be aware that each directive instance will write it's own temp.callMethod function to the parent scope's temp property.

Solution 2

You should define a service that will store the shared values and inject it in the controller and the directive

Service definition

app.factory('sharedData', function () {
    return {
        directiveScope: null
    };
});

Controller definition

app.controller('MainCtrl', ['$scope', 'sharedData', function ($scope, sharedData) {
    ...
    $scope.callMethod = function () {
        $scope.term = "Changed term"
        $scope.bar = "Changed bar";
        sharedData.directiveScope = "Changed directive_scope value";
    }
}]);

Directive definition

app.directive('sampleDirective', ['sharedData', function (sharedData) {
    return {
        scope: {
            callMethod: '='
        },
        link: function ($scope, $element) {
            $scope.sharedData = sharedData;
        },
        template: '<div><a ng-click="callMethod()">click_here</a> <span> <b>{{bar}}</b></span>   <span> <b>{{term}}</b></span>  <span> <b>{{sharedData.directiveScope}}</b></span></div>'
    };
}]);

Notice that I changed the output in the template to {{sharedData.directiveScope}}

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 : How to change scope value from isolated scope within directive

From Dev

Change a scope value in directive

From Dev

Change controller scope value from directive AngularJS

From Dev

How to write a directive to change isolate scope value of another directive

From Dev

change controller $scope from a directive

From Dev

Change Controller variable value from change in isolate scope directive

From Dev

Changing a scope value from a directive

From Dev

AngularJS : Change parent scope value from custom directive

From Dev

How to change ng-model scope value in custom Angular directive?

From Dev

AngularJS : Watch scope value of a injected service changed in one directive from another directive

From Dev

Angular how to change controller scope from onclick directive

From Dev

how can i change $scope property of controller from inside directive?

From Dev

How use isolated scope action from one directive to another

From Dev

how to communicate from one directive to another directive

From Dev

How to pass value back from angularjs directive scope with javascript event

From Dev

How to pass value back from angularjs directive scope with javascript event

From Dev

Change the ngModel value from a directive

From Dev

Change directive value from controller

From Dev

Use, in child directive, value from parent directive and $scope

From Dev

Use, in child directive, value from parent directive and $scope

From Dev

Not able to update the `scope` value from directive function

From Dev

Not able to get the value from the `directive` scope

From Dev

Get value from directive to $scope in controller

From Dev

Unable to get value from scope using directive

From Dev

Not able to get the value from the `directive` scope

From Dev

How do I access a directive scope from a parent directive/controller?

From Dev

How to set a default value in an AngularJS Directive Scope?

From Dev

How to modify a scope's value within a directive

From Dev

How to pass scope value to the directive controller

Related Related

  1. 1

    AngularJS : How to change scope value from isolated scope within directive

  2. 2

    Change a scope value in directive

  3. 3

    Change controller scope value from directive AngularJS

  4. 4

    How to write a directive to change isolate scope value of another directive

  5. 5

    change controller $scope from a directive

  6. 6

    Change Controller variable value from change in isolate scope directive

  7. 7

    Changing a scope value from a directive

  8. 8

    AngularJS : Change parent scope value from custom directive

  9. 9

    How to change ng-model scope value in custom Angular directive?

  10. 10

    AngularJS : Watch scope value of a injected service changed in one directive from another directive

  11. 11

    Angular how to change controller scope from onclick directive

  12. 12

    how can i change $scope property of controller from inside directive?

  13. 13

    How use isolated scope action from one directive to another

  14. 14

    how to communicate from one directive to another directive

  15. 15

    How to pass value back from angularjs directive scope with javascript event

  16. 16

    How to pass value back from angularjs directive scope with javascript event

  17. 17

    Change the ngModel value from a directive

  18. 18

    Change directive value from controller

  19. 19

    Use, in child directive, value from parent directive and $scope

  20. 20

    Use, in child directive, value from parent directive and $scope

  21. 21

    Not able to update the `scope` value from directive function

  22. 22

    Not able to get the value from the `directive` scope

  23. 23

    Get value from directive to $scope in controller

  24. 24

    Unable to get value from scope using directive

  25. 25

    Not able to get the value from the `directive` scope

  26. 26

    How do I access a directive scope from a parent directive/controller?

  27. 27

    How to set a default value in an AngularJS Directive Scope?

  28. 28

    How to modify a scope's value within a directive

  29. 29

    How to pass scope value to the directive controller

HotTag

Archive