$index在ng-repeat中不存在-答案在我的原始帖子中
$index does not exist in ng-repeat - the answer is inside my original post
当试图将$index绑定到复选框时,我在ng-repeat中遇到了一个问题。首先,赋值一个数组,如下所示:
var_dump from php:
array(3) {
[0] => array(9) {
["first_name"] => string(3)
"emp" ["last_name"] => string(5)
"loyee" ["username"] => string(4)
"emp1" ["email"] => string(22)
"blabla@gmail.com" ["id"] => string(3)
"539" ["rights_type"] => string(1)
"3" ["editProfile"] => bool(true)["sendMessage"] => bool(true)["editApps"] => bool(false)
}[1] => array(9) {
["first_name"] => string(3)
"one" ["last_name"] => string(4)
"more" ["username"] => string(4)
"emp2" ["email"] => string(19)
"blabla@gmail.com" ["id"] => string(3)
"540" ["rights_type"] => string(1)
"0" ["editProfile"] => bool(false)["sendMessage"] => bool(false)["editApps"] => bool(false)
}["isEmpty"] => bool(false)
}
我的代码是这样的:
<tr ng-repeat="employee in employees.result track by $index" ng-if="employee.username">
<th class="col-md-1" scope="row">{{employee.username}} - ({{employee.first_name}} {{employee.last_name}})</th>
<td class="col-md-1"><input type="checkbox" ng-model="employees.result.$index.editProfile"></td>
<td class="col-md-1"><input type="checkbox" ng-model="employees.result.$index.sendMessage"></td>
<td class="col-md-1"><input type="checkbox" ng-model="employees.result.$index.editApps"></td>
</tr>
该对象被附加到控制器的$作用域。重要的是,ng-repeat确实有效,我得到了名字,用户名等的正确值,只是无法得到$index。当我试图打印{{$index}}时,我什么也得不到。我试图从数组中删除"isEmpty"键,但它也没有帮助。什么好主意吗?
Roee-distler最初将他们的解决方案编辑到问题中
我要改变的只是员工。结果发给雇员,它实际上更新了雇员。结果(数组的每个单元格分别),因此它看起来像:
<tr ng-repeat="employee in employees.result track by $index" ng-if="employee.username">
<th class="col-md-1" scope="row">{{employee.username}} - ({{employee.first_name}} {{employee.last_name}})</th>
<td class="col-md-1"><input type="checkbox" ng-model="employee.editProfile"></td>
<td class="col-md-1"><input type="checkbox" ng-model="employee.sendMessage"></td>
<td class="col-md-1"><input type="checkbox" ng-model="employee.editApps"></td>
</tr>
更新
我认为问题在于,当您在当前正在查看单个employee
的循环中引用employees
时
正如我提到的,我从未使用过$index
,所以我不确定,但我也会仔细检查你是否需要在那里引用它(因为你的工作代码根本没有引用它)
在你的代码中尝试更改:
<td class="col-md-1"><input type="checkbox" ng-model="employees.result.$index.editProfile"></td>
<td class="col-md-1"><input type="checkbox" ng-model="employee.result.editProfile"></td>
在确实需要引用的情况下,我会想象引用看起来更像:
<input type="checkbox" ng-model="employee.result[$index].editProfile">
作为索引通常被引用。
相关文章:
- 为什么不't Javascript对我的输入值进行了一些重新检查
- 可以't让我的if语句处理js中的html表单输入
- 我的单元测试选项是什么
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 我的职位回报太快了,如何做出承诺
- 我的jQuery插件参数没有正确启动,遇到了问题
- 如何识别我的网站中的慢速设备
- 如何将JSON数据导入我的ejs模板
- JQuery添加元素需要在我的js之前再次添加JQuery脚本
- 知道为什么我的旋转木马不会自动更改图片吗
- 我的模板未被解析
- 模板页面 css 覆盖了我的原始 css
- 我可以在 ReactJS 中排除原始元素的子元素吗?
- 点击我应该得到原始大小的图像
- 有可能用我的新上下文而不是原始的应用程序上下文来伪造WebView的构造函数吗
- 如果我在 JavaScript 中拼接克隆的数组,为什么我的原始数组会被拼接
- 我怎么得到原始图像的尺寸
- $index在ng-repeat中不存在-答案在我的原始帖子中
- 我的Javascript购物车代码如何改进?请原谅原始代码
- 为什么我的 Angular 是转义 HTML,我怎样才能让它传递受信任的原始数据