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

Vishnu Sureshkumar

Consider the following scenario.

There are two directives which is independent of each other and I want to share the scope between these two directives. Since they are not nested, I cant use a controller in the directives to share the scope. So I have created a service and injected the service to both of these directives.

So when the value of the service is changed in one directive, I need the same to be updated in the other directive. How can I achieve this? I want this done right, the angular way, with no anti-patterns.

This is what I have tried so far

.directive('d1', ['cData', function() {
  return {
    link: function() {
       // this watch is not working
       scope.$watch( function() {
            return cData.Value;
       }, function(newValue, oldValue) {
            alert(newValue);
       })
       }
  }
})
.directive('d2', ['cData', function() {
  return {
    link: function() {
      element.on('click', function() {              
        cData.Value = false
        scope.$digest();
      });
      // A watch here is working, I have commented it out
      /*scope.$watch( function() {
            return cd.showStatus;
      }, function(newValue, oldValue) {
            alert(newValue)
      })*/
    }
  }
}])
.service('cData', function() {
  this.Value = false;
})

I am updating the value of the service in click event of directive d2 and I need to trigger the watch in the directive d1. May be this is not working as the scopes are different for two directives. How can I achieve this, any help is greatly appreciated.

UPDATE

Template for directive d1

template : '<div ng-class="{show:cData.Value==true}"></div>'

To update {show:cData.Value==true}

I have to again call scope.$apply() in the event listener of directive d1

Hope this is the right approach.

Andrei CACIO

You are on the right path but, I would recommend using $broadcast instead of adding a new $watch. Adding a new watcher is more of a local scope solution (in my point of view at least). So it all boils down to sharing data between two independent objects. You can achieve this like so:

.directive('d1', ['cData', function() {
  return {
    link: function(scope) {
       // this watch is not working
       scope.$on('data:changed', function(e, data) {
          alert(data);
       }
       /*
       scope.$watch( function() {
            return cData.Value;
       }, function(newValue, oldValue) {
            alert(newValue);
       }) */
       }
  }
})
.directive('d2', ['cData', '$rootScope', function() {
  return {
    link: function() {
      element.on('click', function() { 
        $rootScope.$broadcast('data:changed', { value: false });             
        // cData.Value = false
        // scope.$digest();
      });
      // A watch here is working, I have commented it out
      /*scope.$watch( function() {
            return cd.showStatus;
      }, function(newValue, oldValue) {
            alert(newValue)
      })*/
    }
  }

}])

From my point of view it's the cleanest way to achieve communication between independent objects/directives without much hassle. And the code is pretty clean and decoupled.

What do you think?

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: $watch not firing when value changed in directive

From Dev

AngularJS $watch controller variable from a directive with scope

From Dev

AngularJS directive scope not updating when value changed outside of AngularJS

From Dev

AngularJS : $watch within directive is not working when $rootScope value is changed

From Dev

AngularJS : $watch within directive is not working when $rootScope value is changed

From Dev

Change controller scope value from directive AngularJS

From Dev

Watch not firing the second time the scope is changed in directive

From Dev

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

From Dev

AngularJs directive not updating another directive's scope

From Dev

Angularjs Uncaught ReferenceError on injected service into directive

From Dev

AngularJs calling Controller from another one in directive

From Dev

Pass value from child directive isolated scope to parent directive isolated scope and trigger watch of parent

From Dev

Copy scope property from another isolated scope using one directive

From Dev

Angularjs - watch all directive scope attributes?

From Dev

Angularjs - watch all directive scope attributes?

From Dev

angularjs directive not upating with scope.$watch

From Dev

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

From Dev

AngularJs directive, scope with value and function

From Dev

AngularJs directive, scope with value and function

From Dev

$scope.$watch not catching the value modified by directive

From Dev

Access scope from another directive

From Dev

How to call a directive from an another directive in AngularJS?

From Dev

add a directive to html from another directive in angularJS

From Dev

Watch on controller scope from directive link function

From Dev

How use isolated scope action from one directive to another

From Dev

AngularJS: Binding a service value to a directive

From Dev

how to communicate from one directive to another directive

From Dev

Changing a scope value from a directive

From Dev

AngularJS : Change parent scope value from custom directive

Related Related

  1. 1

    AngularJS: $watch not firing when value changed in directive

  2. 2

    AngularJS $watch controller variable from a directive with scope

  3. 3

    AngularJS directive scope not updating when value changed outside of AngularJS

  4. 4

    AngularJS : $watch within directive is not working when $rootScope value is changed

  5. 5

    AngularJS : $watch within directive is not working when $rootScope value is changed

  6. 6

    Change controller scope value from directive AngularJS

  7. 7

    Watch not firing the second time the scope is changed in directive

  8. 8

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

  9. 9

    AngularJs directive not updating another directive's scope

  10. 10

    Angularjs Uncaught ReferenceError on injected service into directive

  11. 11

    AngularJs calling Controller from another one in directive

  12. 12

    Pass value from child directive isolated scope to parent directive isolated scope and trigger watch of parent

  13. 13

    Copy scope property from another isolated scope using one directive

  14. 14

    Angularjs - watch all directive scope attributes?

  15. 15

    Angularjs - watch all directive scope attributes?

  16. 16

    angularjs directive not upating with scope.$watch

  17. 17

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

  18. 18

    AngularJs directive, scope with value and function

  19. 19

    AngularJs directive, scope with value and function

  20. 20

    $scope.$watch not catching the value modified by directive

  21. 21

    Access scope from another directive

  22. 22

    How to call a directive from an another directive in AngularJS?

  23. 23

    add a directive to html from another directive in angularJS

  24. 24

    Watch on controller scope from directive link function

  25. 25

    How use isolated scope action from one directive to another

  26. 26

    AngularJS: Binding a service value to a directive

  27. 27

    how to communicate from one directive to another directive

  28. 28

    Changing a scope value from a directive

  29. 29

    AngularJS : Change parent scope value from custom directive

HotTag

Archive