Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角JS根镜_Javascript_Angularjs_Angularjs Directive_Angularjs Scope_Angular Ui Bootstrap - Fatal编程技术网

Javascript 角JS根镜

Javascript 角JS根镜,javascript,angularjs,angularjs-directive,angularjs-scope,angular-ui-bootstrap,Javascript,Angularjs,Angularjs Directive,Angularjs Scope,Angular Ui Bootstrap,我正在视图层使用angular js。我需要使用一些全局变量,这些变量将由我的应用程序中的所有控制器方法使用和修改: var app = angular.module('myWebservice', ['ngCookies']).run( function($rootScope) { $rootScope.authToken = null; }); app.controller('UserController', function($cookieStore, $s

我正在视图层使用angular js。我需要使用一些全局变量,这些变量将由我的应用程序中的所有控制器方法使用和修改:

 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 
    }
});

我是Angular JS的新手,我也尝试过使用这个服务,但我不知道为什么我不能访问全局变量(authToken)。请帮助我,我在这一点上被卡住了…提前感谢…

我不确定您的应用程序是如何工作的,但看起来您有异步调用的问题(虽然$http正在工作,但您正在尝试访问尚未设置的变量)。请看这个答案。也许这就是你的案子?我还应该注意到,使用$rootScope总是一个坏主意。尽量避免。您应该改用服务,它也是一个单例对象,并且总是一种更好、可维护的方法

我需要使用一些全局变量,这些变量将由应用程序中的所有控制器方法使用和修改

实现这一点的最佳方法是使用服务,但如果希望将其留在$rootScope,则不必这样做


我也尝试过使用服务,但同样的结果“未定义”

因为对authToken的调用是异步的,所以控制器可以在令牌从远程服务器返回之前运行

您可以将令牌检索逻辑作为“解析”字段添加到路由定义中,如下所示:

.config(function($routeProvider, $locationProvider) {
  $routeProvider
   .when('/your/route', {
    templateUrl: 'book.html',
    controller: 'BookController',
    resolve: {      
      token: function() {
        //return a promise with the token here
      }
    }
  })
但是,这在您的情况下并不好,因为您必须为每个路由设置“解析”。如果您使用的是支持嵌套路由的,则只需将“解析”添加到根状态/路由。这是我在我的项目中一直使用的

如果你不使用,还有另一种方法。您可以将$http.post的返回值分配给$rootScope中的变量,例如$rootScope.tokenPromise。 返回的$http.post是一个,您可以使用'then;方法注册回调,这些回调将接收单个参数–表示响应的对象

在您的控制器中,您将放置一些额外的行,您将像这样异步获取令牌

app.controller('MerchantController', function($cookieStore, $scope, $location,
    $routeParams, $http, $timeout, $rootScope) {

    $rootScope.tokenPromise.then( function(token){
        console.log(token); //depends on the returned data structure

    } )

});

在应用程序“运行”块中,必须在$rootScope.tokenPromise处创建一个承诺。您可以使用$http检索令牌并在run块中获取承诺,也可以创建挂起的承诺并在其他位置检索令牌,之后,您可以解决承诺。

这可能是因为您试图在通过
$http
post设置之前访问
authToken
。首先我调用login,在login方法中我获取authToken,然后在设置为rootScope之后,如果我登录,我获取authToken的值。我的意思是您可能正在尝试tp
console.log
MerchantController
中完成HTTP请求之前,登录
UserController
中的authToken。不,不,实际上,当我使用login执行完之后,我只调用getRootScopeValues函数。当用户登录时,他单击我编写的链接ng click=“getRootScopeValues()”。我也可以相应地查看服务器日志。
$http.post()
返回一个承诺,这意味着如果authToken是异步设置的(即在调用
login()
完成之后)。因此…+1.作为单例的服务肯定是可行的。我也尝试过使用服务,但同样的结果“未定义”使用rootScope或服务在这里不是问题。使用异步调用的问题。您应该使用$http回调或承诺来处理结果。app.service('shared',function(){var sharedData={authToken:''};return{setAuthToken:function(v){sharedData.authToken=v;},update:function(){return sharedData;}}});