ng-repeat错误。无法读取未定义的属性
我有两个错误说:Cannot read property 'active' of undefined
和Cannot read property 'boss' of undefined
而我试图在我的用户数组上使用ng-repeat
。ng-repeat错误。无法读取未定义的属性
在控制器中我用下面的代码:
$scope.users = [
{
id: 1,
name: 'John',
birthday: '06.07.2008',
city: 'Budapest',
active: false,
boss: false
},
{
id: 2,
name: 'Mary',
birthday: '01.02.2003',
city: 'Berlin',
active: false,
boss: true
},
{
id: 3,
name: 'James',
birthday: '04.05.2006',
city: 'Vienna',
active: false,
boss: false
}
];
$scope.isActive = function (id) {
return $scope.users[id].active;
}
$scope.isBoss = function (id) {
console.log($scope.users[id]);
return $scope.users[id].boss;
}
在视图我有以下代码:
<thead>
<tr>
<th>#</th>
<th>Username</th>
<th>Birthday</th>
<th>City</th>
<th>Active</th>
<th>Boss</th>
</tr>
</thead>
<tbody>
<div>
<tr ng-click="goToProfile(user.id)" ng-repeat="user in users">
<td>{{user.id}}</td>
<td>{{user.name}}</td>
<td>{{user.birthday}}</td>
<td>{{user.city}}</td>
<td>
<input type="checkbox" name="active" ng-checked="{ checked: isActive(user.id) }">
</td>
<td>
<input type="radio" name="boss" ng-checked="{ checked: isBoss(user.id) }">
</td>
</tr>
</div>
</tbody>
在结束它打印的表格以正确的方式也抛出这些错误。我错过了什么?
而且,这里的渲染结果:
<tbody>
<!-- ngRepeat: user in users -->
<tr ng-click="goToProfile(user.id)" ng-repeat="user in users" class="ng-scope">
<td class="ng-binding">1</td>
<td class="ng-binding">John</td>
<td class="ng-binding">06.07.2008</td>
<td class="ng-binding">Budapest</td>
<td>
<input type="checkbox" name="active" ng-checked="{ checked: isActive(user.id) }" checked="checked">
</td>
<td>
<input type="radio" name="boss" ng-checked="{ checked: isBoss(user.id) }" checked="checked">
</td>
</tr>
<!-- end ngRepeat: user in users -->
<tr ng-click="goToProfile(user.id)" ng-repeat="user in users" class="ng-scope">
<td class="ng-binding">2</td>
<td class="ng-binding">Mary</td>
<td class="ng-binding">01.02.2003</td>
<td class="ng-binding">Berlin</td>
<td>
<input type="checkbox" name="active" ng-checked="{ checked: isActive(user.id) }" checked="checked">
</td>
<td>
<input type="radio" name="boss" ng-checked="{ checked: isBoss(user.id) }" checked="checked">
</td>
</tr>
<!-- end ngRepeat: user in users -->
<tr ng-click="goToProfile(user.id)" ng-repeat="user in users" class="ng-scope">
<td class="ng-binding">3</td>
<td class="ng-binding">James</td>
<td class="ng-binding">04.05.2006</td>
<td class="ng-binding">Vienna</td>
<td>
<input type="checkbox" name="active" ng-checked="{ checked: isActive(user.id) }">
</td>
<td>
<input type="radio" name="boss" ng-checked="{ checked: isBoss(user.id) }">
</td>
</tr>
<!-- end ngRepeat: user in users -->
</tbody>
那是因为你的isActive
和isBoss
功能被传递user.id
。我观察到user.id
从id 1开始,同时在这些函数中,您尝试使用传入的索引访问$scope.users
。例如,函数中user.id
的user.id
会尝试访问$scope.users[3].active
。但是,实际上是没有索引3(代表$scope.users
数组中的第四个项目,这是undefined
)
更改..isActive(user.id)
到...isActive($index)
和你...isBoss(user.id)
到...isBoss($index)
非常感谢。我在帖子上完成了编辑,添加了一些后遗症。看看你会。 –
您应该为更新打开另一个问题。这与最初的问题无关 – Ladmerc
请不要编辑了问题补充一个不同的问题;如果这个问题得到解答,您应该接受答案,并在新问题中提出一个新问题(如有必要,请参考此问题)。改变线程中的基本问题使得未来的读者很难确定哪个答案解决了哪个问题。 – Claies