listener - AngularJS - Refresh all instances of the app on $emit -
i've angularjs app. i've page following template , controller:
<div ng-controller="parentctrl"> <p>{{food}}</p> <div ng-controller="childctrl"> <button ng-click="onbtnclk()">{{buttontitle}}</button> </div> </div>
the 2 controllers:
app.controller('parentctrl', parentctrl); parentctrl.$inject = ['$scope','$rootscope']; function parentctrl($scope,$rootscope) { $rootscope.food = "hi" $rootscope.$on("update_parent", function(event,msg) { $rootscope.food = msg; console.log("witnessed") }); } app.controller('childctrl', childctrl); childctrl.$inject = ['$scope','$rootscope']; function childctrl($scope,$rootscope) { $scope.buttontitle="update parent" $scope.onbtnclk = function() { $rootscope.$emit("update_parent","updatedyy"); console.log($rootscope) }; }
so basically, i've parent , child controller; clicking button in child controller emits event message "updatedyy". picked $on in parent controller sets value of $rootscope.food message value.
now question is, possible see value change of variable in instances of app? example, if i've app (running on tomcat server) open in 2 tabs in browser, , click on button - possible changed value in other tab well?
Comments
Post a Comment