Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.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
尝试从AngularJS中的控制器中的我的服务访问变量_Angularjs_Service_Controller - Fatal编程技术网

尝试从AngularJS中的控制器中的我的服务访问变量

尝试从AngularJS中的控制器中的我的服务访问变量,angularjs,service,controller,Angularjs,Service,Controller,我想获得有关当前登录用户的一些信息,我已将变量存储在currentUser中,但当我尝试从控制器访问它时,没有显示任何信息 authentication.service.js: (function () { 'use strict'; angular .module('thinkster.authentication.services') .factory('Authentication', Authentication); Authentication.$in

我想获得有关当前登录用户的一些信息,我已将变量存储在
currentUser
中,但当我尝试从控制器访问它时,没有显示任何信息

authentication.service.js:

(function () {
  'use strict';

  angular
    .module('thinkster.authentication.services')
    .factory('Authentication', Authentication);


  Authentication.$inject = ['$cookies', '$http'];

  /**
  * @namespace Authentication
  * @returns {Factory}
  */
  function Authentication($cookies, $http) {
    /**
    * @name Authentication
    * @desc The Factory to be returned
    */


    var Authentication = {
      login: login,
      register: register,
      getAuthenticatedAccount: getAuthenticatedAccount,
      isAuthenticated: isAuthenticated,
      setAuthenticatedAccount: setAuthenticatedAccount,
      unauthenticate: unauthenticate,
      currentUser: ''
    };



    return Authentication;

    ////////////////////
/**
*@name寄存器
*@desc尝试注册新用户
*@param{string}username用户输入的用户名
*@param{string}password用户输入的密码
*@param{string}通过电子邮件发送用户输入的电子邮件
*@returns{Promise}
*@memberOf thinkster.authentication.services.authentication
*/
功能注册(电子邮件、密码、用户名){
返回$http.post(“/api/v1/accounts/”{
用户名:用户名,
密码:密码,
电邮:电邮,,
})。然后(registerSuccessFn,RegisterErrorn);
函数寄存器successfn(数据-,s-,t-,a-,t-,u-,s-,h-,e-,a-,e-,r-,s-,c-,o-,n-,f-,i-){
身份验证。登录(电子邮件、密码)
};
函数寄存器errorfn(数据-,s-,t-,a-,t-,u-,s-,h-,e-,a-,e-,r-,s-,c-,o-,n-,f-,i-){
console.log(数据)
}
}
功能登录(电子邮件、密码){
返回$http.post('/api/v1/auth/login/'{
电邮:电邮,,
密码:密码
})。然后(登录成功fn、登录人fn);
函数登录成功fn(数据-,s-,t-,a-,t-,u-,s-,h-,e-,a-,e-,r-,s-,c-,o-,n-,f-,i-){
Authentication.setAuthenticatedAccount(data.data);
//将数据传递到仪表板控制器
window.location='/dashboard'
Authentication.currentUser+=data.config.data.email
console.log(Authentication.currentUser)
}
函数loginErrorFn(数据、状态、标题、配置){
console.error('Failed');
console.log(数据)
}
}})();
dashboard.controller.js

//仪表板控制器

(function() {
    'use strict';

    angular
      .module('thinkster.authentication.controllers')
      .controller('DashboardController', DashboardController);

    DashboardController.inject = ['$location, $scope', 'Authentication'];

    function DashboardController($location, $scope, Authentication){
        var vm = this;
        vm.user_info = user_info
        //vm.user_info2 = user_info

        function user_info() {
            return Authentication.currentUser
    }
    }

})();

我正在尝试更新
loginsucessfn()
中的
currentUser
,它在服务中成功更新。我可以在控制台中看到这一点,但当我尝试在控制器中引用它时就看不到了。提前感谢

首先,为什么要使用+=

Authentication.currentUser+=data.config.data.email

使用=

  • Authentication.currentUser=data.config.data

  • Authentication.currentUser=data.config.data.email

  • 第二个用户信息是一个函数,所以您必须调用它,更改

    vm.user\u info=user\u info
    to
    vm.user\u info=user\u info()

    此外,为了可读性和一致性,我还对函数使用camelCase,对对象字段使用under_score

    范例

    function userInfo() { // camelCase 
       return Authentication.currentUser;
    }
    
    var vm = this
    vm.user_info = userInfo() // under_score object
    

    你是说给你我自己的plunker?仪表板控制器会立即访问身份验证。currentUser,但服务只有在服务器响应后才设置该变量。嘿,谢谢你的回复,我想知道我是否必须从我的登录控制器获取
    currentUser
    ,因为这样我可以让它在promise中返回值
    。然后
    方法返回一个新的promise,解析为返回到
    的内容。然后
    块。当
    .then
    块省略了return语句时,函数返回
    未定义的
    。派生承诺将解析为
    未定义
    。此外,被拒绝的承诺将被转换为已履行的承诺,该承诺将解析为未定义的。是因为我在请求发出之前正在检索
    currentUser
    ?是的,这一定是问题所在,因为当我为
    currentUser
    硬编码一个值时,它工作了。你必须在检索数据点之前设置值(完成请求和操作)。好的,我该怎么做?在发出请求后,如何检索该值?使用
    。然后()