Javascript 关于'的困惑;这';角度中的关键字';控制器组件';语法

Javascript 关于'的困惑;这';角度中的关键字';控制器组件';语法,javascript,angularjs,Javascript,Angularjs,我有一个父控制器,UserEditCtrl和一个子控制器,EditUserCtrl。在父控制器内部,我通过服务拉入一个用户对象: userMgmtSvc.user(scope.editUserId).then(function(data) { this.user = data; }); 然后我想将我的用户对象的属性设置为另一个变量 this.selectedRoles = this.user.roles; 但这带来了一个错误: Cannot read property 'user' of

我有一个父控制器,
UserEditCtrl
和一个子控制器,
EditUserCtrl
。在父控制器内部,我通过服务拉入一个用户对象:

userMgmtSvc.user(scope.editUserId).then(function(data) {
  this.user = data;
});
然后我想将我的用户对象的属性设置为另一个变量

this.selectedRoles = this.user.roles;
但这带来了一个错误:

Cannot read property 'user' of undefined.
对于如何引用使用
this设置的对象,我感到困惑。例如,如何对对象执行console.log操作?因为
console.log('user',this.user)返回未定义的:

user undefined
这是父控制器:

(
  function (app) {
    /* @fmt: off */
    'use strict';

    // @fmt:on
    app.controller('UserEditCtrl', ['$scope', '$http', 'userMgmtSvc', 'createUserSvc', 'authSvc', '$state', '$timeout', '$location', '_',
      function (scope, http, userMgmtSvc, createUserSvc, authSvc, state, timeout, location, _) {

      userMgmtSvc.user(scope.editUserId.id || sessionStorage.getItem('editUser')).then(function(data) {
        this.user = data;
        console.log('user', this.user);

      // GET states
      createUserSvc.states().then(function(data) {
          this.states = data;
          console.log(this.states);
      });

      // GET countries
        createUserSvc.countries().then(function(data) {
        this.countries = data;
      });

      // GET roles
      createUserSvc.roles().then(function(data) {
        this.roles = data;
      });

      // GET insurance groups
      createUserSvc.insuranceGroups().then(function(data) {
        this.insuranceGroups = data;
      });

      this.selectedRoles = this.user.roles;
    });

 }]);

}(window.app)
);

这是一个非常基本的错误,当您在回调中使用
This
引用当前上下文时,您不知道执行上下文,最终在其他地方设置值

为了避免陷入这个问题,当您的控制器启动时,只需将
this
(控制器实例的上下文)设置为一个变量,并设置该变量上的所有内容。不要假设这将是什么

 .controller('crtl',[deps..., function(...) {
       //Set this
       var vm = this; //Always use this cached variable have seen a commonly used name of vm

       //............... 
       //...............
       userMgmtSvc.user(scope.editUserId).then(function(data) {
         vm.user = data;
       });

       //...............
       vm.selectedRoles = vm.user.roles

   }
使用angular.bind或es5 function.bind创建绑定函数(函数引用预先绑定了指定的上下文)还有许多其他方法可以做到这一点,但最简单的方法是使用缓存上下文

当您使用typescript时,您可以使用
=>
(胖箭头)语法,因为ES5模式下的typescript实际上会转换此语法

      userMgmtSvc.user(scope.editUserId).then((data) => {
         this.user = data;
      });
致:

    var _that = this;
    userMgmtSvc.user(scope.editUserId).then((data) => {
         _that.user = data;
     });
将成为语言本身的一部分(使用ES6规范)。因此,使用ES6,您可以安全地编写:-

      userMgmtSvc.user(scope.editUserId).then((data) => {
         this.user = data;
      });

这是一个非常基本的错误,当您在回调中使用
This
引用当前上下文时,您不知道执行上下文,最终在其他地方设置值

为了避免陷入这个问题,当您的控制器启动时,只需将
this
(控制器实例的上下文)设置为一个变量,并设置该变量上的所有内容。不要假设这将是什么

 .controller('crtl',[deps..., function(...) {
       //Set this
       var vm = this; //Always use this cached variable have seen a commonly used name of vm

       //............... 
       //...............
       userMgmtSvc.user(scope.editUserId).then(function(data) {
         vm.user = data;
       });

       //...............
       vm.selectedRoles = vm.user.roles

   }
使用angular.bind或es5 function.bind创建绑定函数(函数引用预先绑定了指定的上下文)还有许多其他方法可以做到这一点,但最简单的方法是使用缓存上下文

当您使用typescript时,您可以使用
=>
(胖箭头)语法,因为ES5模式下的typescript实际上会转换此语法

      userMgmtSvc.user(scope.editUserId).then((data) => {
         this.user = data;
      });
致:

    var _that = this;
    userMgmtSvc.user(scope.editUserId).then((data) => {
         _that.user = data;
     });
将成为语言本身的一部分(使用ES6规范)。因此,使用ES6,您可以安全地编写:-

      userMgmtSvc.user(scope.editUserId).then((data) => {
         this.user = data;
      });

这是一个非常基本的错误,当您在回调中使用
This
引用当前上下文时,您不知道执行上下文,最终在其他地方设置值

为了避免陷入这个问题,当您的控制器启动时,只需将
this
(控制器实例的上下文)设置为一个变量,并设置该变量上的所有内容。不要假设这将是什么

 .controller('crtl',[deps..., function(...) {
       //Set this
       var vm = this; //Always use this cached variable have seen a commonly used name of vm

       //............... 
       //...............
       userMgmtSvc.user(scope.editUserId).then(function(data) {
         vm.user = data;
       });

       //...............
       vm.selectedRoles = vm.user.roles

   }
使用angular.bind或es5 function.bind创建绑定函数(函数引用预先绑定了指定的上下文)还有许多其他方法可以做到这一点,但最简单的方法是使用缓存上下文

当您使用typescript时,您可以使用
=>
(胖箭头)语法,因为ES5模式下的typescript实际上会转换此语法

      userMgmtSvc.user(scope.editUserId).then((data) => {
         this.user = data;
      });
致:

    var _that = this;
    userMgmtSvc.user(scope.editUserId).then((data) => {
         _that.user = data;
     });
将成为语言本身的一部分(使用ES6规范)。因此,使用ES6,您可以安全地编写:-

      userMgmtSvc.user(scope.editUserId).then((data) => {
         this.user = data;
      });

这是一个非常基本的错误,当您在回调中使用
This
引用当前上下文时,您不知道执行上下文,最终在其他地方设置值

为了避免陷入这个问题,当您的控制器启动时,只需将
this
(控制器实例的上下文)设置为一个变量,并设置该变量上的所有内容。不要假设这将是什么

 .controller('crtl',[deps..., function(...) {
       //Set this
       var vm = this; //Always use this cached variable have seen a commonly used name of vm

       //............... 
       //...............
       userMgmtSvc.user(scope.editUserId).then(function(data) {
         vm.user = data;
       });

       //...............
       vm.selectedRoles = vm.user.roles

   }
使用angular.bind或es5 function.bind创建绑定函数(函数引用预先绑定了指定的上下文)还有许多其他方法可以做到这一点,但最简单的方法是使用缓存上下文

当您使用typescript时,您可以使用
=>
(胖箭头)语法,因为ES5模式下的typescript实际上会转换此语法

      userMgmtSvc.user(scope.editUserId).then((data) => {
         this.user = data;
      });
致:

    var _that = this;
    userMgmtSvc.user(scope.editUserId).then((data) => {
         _that.user = data;
     });
将成为语言本身的一部分(使用ES6规范)。因此,使用ES6,您可以安全地编写:-

      userMgmtSvc.user(scope.editUserId).then((data) => {
         this.user = data;
      });

然后
回调中,
可能不涉及控制器。看见此外,在您试图读取
this.user.roles
时,
then
回调很可能尚未执行,因此无法设置值,即使
this
引用了正确的对象。为了更好地帮助您,您必须提供一个完整的示例,如下所述:Felix所说的。您可以始终将
.bind()
中的函数
then()
绑定到
this
,以便它是正确的关键字。在
then
回调中,
this
可能不引用控制器。看见此外,在您试图读取
this.user.roles
时,
then
回调很可能尚未执行,因此无法设置值,即使
this
引用了正确的对象。为了更好地帮助您,您必须提供一个完整的示例,如下所述:Felix所说的。您可以始终将
.bind()
中的函数
then()
绑定到
this
,以便它是正确的关键字。在
then
回调中,
this
可能不引用控制器。看见此外,在您试图读取
this.user.roles
时,
then
回调很可能尚未执行,因此无法设置值,即使
this