Angularjs 重复错误。Can';不读取未定义的属性
我有两个错误,它们是:Angularjs 重复错误。Can';不读取未定义的属性,angularjs,angularjs-ng-repeat,ng-repeat,Angularjs,Angularjs Ng Repeat,Ng Repeat,我有两个错误,它们是:无法读取未定义的的属性“active”和无法读取未定义的的属性“boss”,而我正试图在我的用户数组中使用ng repeat 在控制器中,我使用以下代码: $scope.users = [ { id: 1, name: 'John', birthday: '06.07.2008', city: 'Bu
无法读取未定义的
的属性“active”和无法读取未定义的
的属性“boss”,而我正试图在我的用户数组中使用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>
在视图中,我有以下代码:
$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>
#
用户名
生日
城市
活跃的
老板
{{user.id}
{{user.name}
{{user.birth}
{{user.city}
最后,它以正确的方式打印表,但也会抛出这些错误。我错过什么了吗
这里还有渲染结果:
<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>
1.
约翰
06.07.2008
布达佩斯
2.
玛丽
01.02.2003
柏林
3.
詹姆斯
04.05.2006
维也纳
这是因为您的isActive
和isBoss
函数正在被传递user.id
。我观察到您的user.id
从id 1开始,同时在这些函数中,您试图使用传入的索引访问$scope.users
。例如,您的isActive
函数中3的user.id
将尝试访问$scope.users[3]。active
。但是,实际上没有索引3(表示$scope.users
数组中的第四项,即未定义的)
将您的.isActive(user.id)
更改为…isActive($index)
,将您的…isBoss(user.id)
更改为…isBoss($index)
,非常感谢。我在帖子上做了编辑,添加了一些bug。请看一看。你应该为更新打开另一个问题。与初始问题无关请不要编辑问题以添加其他问题;如果回答了此问题,您应该接受答案并用新问题开始新问题(如有必要,请参考此问题)。更改线程中的基本问题会使未来的读者很难确定哪个答案解决了哪个问题。