2017-05-31 276 views
1

我正在Angular应用程序中工作,并在控制器内工作,我需要迭代对象数组。这是控制器以及参与这种情况下,代码:javascript遍历对象数组

myapp.controller('LoginController', ['$scope', function(scope) { 
    // Users for test 
    this.users = [ 
     { 
      name: 'admin', 
      password: 'admin', 
      role: 'ADMIN' 
     }, 
     { 
      name: 'employee', 
      password: '12345', 
      role: 'EMPLOYEE' 
     } 
    ]; 
    console.dir(this.users); // Prints an array of objects correctly 

    // called when user submits 
    scope.login = function() { 
     console.log('login(). User: '); 
     console.dir(scope.user); // Prints the object with user's input (also correct) 

     var found = false; 

     for(let u of this.users) { 
      console.log('Comparing with:'); 
      console.dir(u); 

      if(u.name == scope.user.name && u.password == scope.user.password) { 
       console.log('Found!'); 
       found = true; 

       // do something else... 
      } 
     } 

     if(!found) { // show error message... } 
    } 
}]); 

的问题是,当我提交登录表单(来电scope.login())我在控制台收到一条错误消息:

TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined 
    at m.scope.login (loginController.js:44) 
    ... 

loginController.js:44对应for(let u of this.users) {一行。我通过网络进行搜索(W3学校,MDN和这个网站),但解决方案并没有为我工作。我已经尝试以下解决方案:

  • for(var u of this.users)
  • var u; for(u in this.users)
  • for(var i = 0; i < this.users.lenght; i++):这改变了错误消息Cannot read property 'length' of undefined

我觉得它的东西简单,但我cant't图搞清楚它是什么(我对Javascript不熟练,对不起)。任何人都可以帮我解决这个问题吗?

在此先感谢您的答案。

+0

是'this.users'不确定? –

+2

你为什么使用'this'?你是否应该不使用'scope'或者只是一个普通的'var'或'let'?因为当你到达函数时,'this'引用函数 – Chifilly

+1

@Chifilly它是'controller as'语法。它倾向于'$ scope' – mhodges

回答

2

范围登录功能内改变,所以变量this是不一样的面前,因为它是该函数内。

scope.login = function() {之前,你可以这样写:

var _this = this;

然后用_this.users.forEach(function(user) {

for (var i = 0; i < _this.users.length; i++)

+1

我会把你的回答看成是正确的,我用'var users = [...]'所以我可以在整个控制器中使用它,并且完美地工作。非常感谢 –

1

scope.login = function() {}内的this环境的变化,因为它是一个对象的方法,thisscope参考。试试这个:

myapp.controller('LoginController', ['$scope', function(scope) { 
    var that = this; // reference to the correct this context 
    // Users for test 
    this.users = [ 
     { 
      name: 'admin', 
      password: 'admin', 
      role: 'ADMIN' 
     }, 
     { 
      name: 'employee', 
      password: '12345', 
      role: 'EMPLOYEE' 
     } 
    ]; 
    console.dir(this.users); // Prints an array of objects correctly 

    // called when user submits 
    scope.login = function() { 
     console.log('login(). User: '); 
     console.dir(scope.user); // Prints the object with user's input (also correct) 

     var found = false; 
     for(let u of that.users) { // use the correct context here 
      console.log('Comparing with:'); 
      console.dir(u); 

      if(u.name == scope.user.name && u.password == scope.user.password) { 
       console.log('Found!'); 
       found = true; 

       // do something else... 
      } 
     } 

     if(!found) { // show error message... } 
    } 
}]); 
+0

您也可以将'this.users'更改为'scope.users',并且所有内容都按预期工作。 – Brian

+0

值得注意的是'that!= $ scope',这个区别在控制器中不会马上引起注意,但会出现在HTML上,同样使用'this'与'$ scope'相对的是相当意见的,我相信使用'this'的理由是在使用'Controller as'语法在HTML中引用它时强制使用点符号(因为不使用点符号*最终会给出意想不到的错误) –

+0

'this'在方法内部一个对象引用父对象。在这种情况下,'$ scope'对象上的方法'this'引用'$ scope'。你是正确的,'this'并不总是'$ scope',但在这种情况下是。 – Brian