javascript - Angular JS Root Scope -


i working angular js @ view layer. need use global variable used , modified controllers method in application:

 var app = angular.module('mywebservice', ['ngcookies']).run(     function($rootscope) {         $rootscope.authtoken = null; });    app.controller('usercontroller', function($cookiestore, $scope, $location,     $routeparams, $http, $timeout, $rootscope) {          $scope.login= function() {      $http.defaults.headers.post = {'accept' : 'application/json',         'content-type' : 'application/json'};      $http.post('/myservise/api/user/login-user', {                         emailid : $scope.username,                         password : $scope.password                      }).success(function(data, status) {                         if (status == 200) {             $rootscope.authtoken = data.object.authtoken;                 }).error(function(data, status) {                                    console.debug(data);});         }// else end  };// end login   app.controller('merchantcontroller', function($cookiestore, $scope, $location,     $routeparams, $http, $timeout, $rootscope) {      $scope.getrootscopevalues = function()      //$scope.getrootscopevalues = function($rootscope)      {         $scope.token = $rootscope.authtoken;         console.log($scope.token);// undefined          console.log($rootscope.authtoken);// undefined      } }); 

i new angular js, have tried use service don't know why not able access global variable (authtoken). please me stuck @ point ....thanks in advance...

i not sure how application works looks have problem of asynchronous calls (while $http working trying access variable not set yet). please @ answer asynchronous calls. maybe case? should note using $rootscope bad idea. try avoid it. should use service instead singleton object , better , maintainable approach.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -