angularjs - Unable to update the rootScope in angular -
i trying send request php file 1 controller in angular , getting response. stored response in rootscope variable initialized in controller.run() method 0.
now trying use updated rootscope in controller in order display response php page. rootscope not getting updated , still showing 0.
here code.
the following run method rootscope initialized
admincontroller.run(function ($rootscope) { $rootscope.n = 0; });
the following code controller 1 requesting response php page , updating rootscope variable.
admincontroller.controller('adminlogincontroller', function($scope,$rootscope,$http,$window) { $scope.logincheck = function(event,user){ var request = $http({ method: "post", url: "../_/php/logincheck.php", headers: { 'content-type': 'application/x-www-form-urlencoded' } }); request.success(function (data) { $rootscope.n=data; $rootscope.$apply(); $window.location.href = 'admin.html#admin_home'; }); } });
here code second controller utilizing rootscope variable. here problem comes rootscope not getting updated , displaying me 0 initialized.
admincontroller.controller('adminhomecontroller', function($scope,$rootscope,$http,$window) { $scope.uname=$rootscope.n; alert($scope.uname); });
can 1 let me know going wrong.
your problem sounds timing issue. communicate between controllers $rootscope
, can set event listen changes.
update root scope:
$rootscope.$broadcast('n', data);
listen changes:
$rootscope.$on('n', function (event, data) { //do new value 'data' });
Comments
Post a Comment