javascript迭代对象数组
我在Angular应用程序中工作,在控制器中,我需要迭代一组对象。这是控制器和这种情况下涉及的代码:javascript迭代对象数组,javascript,angularjs,loops,Javascript,Angularjs,Loops,我在Angular应用程序中工作,在控制器中,我需要迭代一组对象。这是控制器和这种情况下涉及的代码: myapp.controller('LoginController', ['$scope', function(scope) { // Users for test this.users = [ { name: 'admin', password: 'admin', role: 'ADMIN
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(让我们看看这个.users){
行。我在网上搜索过(W3学校、MDN和这个网站也有),但这些解决方案对我不起作用。我已经尝试了以下解决方案:
for(this.users的var u)
var u;用于(此.users中的u)
:这会将错误消息更改为for(var i=0;i
无法读取未定义的属性“length”
提前感谢您的回答。您的
scope.login=function(){}
中的this
上下文会发生变化,因为它是一个对象方法,this
是对scope
的引用。请尝试以下操作:
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... }
}
}]);
登录函数中的作用域正在更改,因此变量
this
与该函数中的变量不同
在scope.login=function(){
之前,您可以编写:
var\u this=this;
然后使用\u this.users.forEach(函数(用户){
或
for(var i=0;i<\u this.users.length;i++)是这个。用户未定义?为什么要使用这个?你不应该使用范围,或者仅仅是一个普通的var
或let
?因为当你使用函数时,这个指的是function@Chifilly它是控制器的
语法。首选$scope
你也可以只更改这个。用户
到范围。用户
一切都会按预期工作。值得注意的是,那!=$scope
,这个差异在控制器中不会立即被注意到,但会在HTML上,也使用这个
来反对$scope
是相当基于意见的,我认为相信使用这个
的一个原因是当在HTML中以控制器作为语法引用它时,强制使用点符号(因为不使用点符号可能并且最终会产生意外错误)this
在对象上的方法中引用父对象。在本例中,this
在$scope
对象上的方法中引用$scope
。this
并不总是$scope
,但在本例中,它确实是。正如您所看到的,angularjs的作用域不是使用this
检索的javascript引用,而不是angularjs处理的用于封装数据的集合(其中this
是子范围,$scope
是父范围)…您可以通过使用浏览器中的调试器在this
上断点来验证这一点,查看它的计算结果,然后查看$scope
的计算结果;您将发现$scope.ctrl
是this
在控制器的顶层所指的内容。注意:当我说$scope.ctrl
时,ctrl
引用控制器的名称,然后控制器为ctrl
语法,当未使用该语法时此在$scope
中找不到(也找不到它的属性)。我认为您的回答是正确的,我使用了var users=[…]
这样我就可以在整个控制器中使用它,并且工作得非常好。非常感谢