javascript - Ng-Click update the service factory value to reflect in other controllers -


i trying make ng-click change value in factory , other controllers in application reflect change.

factory:

app.factory('data', function() {   function changeme(inc) {       return 1;   }   return {         changeme: changeme     }; }); 

controllers:

app.controller('mainctrl', function($scope, data) {   $scope.change = data.changeme();    // want change data.changeme secctrl reflect   // change well.   $scope.addone = function(){     $scope.change = data.changeme() + 1;   } });  app.controller('secctrl', function($scope, data) {   $scope.change = data.changeme(); }) 

html:

<div ng-controller="mainctrl">     <p ng-click=addone()>{{change}}!</p>   </div>    <div ng-controller="secctrl">     <p>{{change}}   </div> 

how can make ng-click changes value in secctrl well. want them both updated in html.

a plunkr of above: http://plnkr.co/edit/4lswmzwapgypcz3hzi72?p=preview

you can move addone function data factory, , store change variable in there well. i've forked plunker: http://plnkr.co/edit/un4sh1ni4jp5gd1ze26d?p=preview


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -