有没有一种方法可以使用angular中的$index作为ng重复中的模型
Is there a way to use $index in angular as the model in an ng-repeat?
我有一个工厂,我正在将值从我的控制器推送到我的控制器。
.factory("Inputs", function(){
var inputs = {
};
inputs.editors = [0];
return inputs;
})
当我像一样循环时,我想使用$index作为模型
<div class="texteditors">
<div class="form-group" ng-repeat="editor in app.inputs.editors">
<textarea class="form-control" name="{{$index}}" ng-model="inputs.editors[{{$index}}]" id="" cols="30" rows="10"></textarea>
</div>
</div>
但它不会评估,我唯一能让它评估的方法是这样的:
但现在它是一个字符串,它评估为输入。editors['0']
然后,我想循环遍历ng模型,并评估它们{{inputs.editors[0]}}添加了多少文本区域。我不确定我的解释是否正确。
如果我使用指令来创建绑定,我将如何评估通过将值推送到工厂而生成的文本区域和模型?
开始:
http://codepen.io/jlowcs/pen/ogJBVm
您的标记出现了一些问题。
您还需要将track by
添加到ng-repeat
,否则每次模型更改时都会失去焦点,因为文本区域是由ng-repeat
重新创建的。
HTML:
<div class="texteditors" ng-controller="MyCtrl as app">
<div class="form-group" ng-repeat="editor in app.inputs.editors track by $index">
<textarea class="form-control" ng-model="app.inputs.editors[$index]" id="" cols="30" rows="10"></textarea>
</div>
<div class="form-group" ng-repeat="editor in app.inputs.editors track by $index">
<div>{{app.inputs.editors[$index]}}</vid>
</div>
</div>
JS:
app.controller('MyCtrl', function($scope, Inputs) {
this.inputs = Inputs;
})
.factory("Inputs", function(){
var inputs = {};
inputs.editors = ['foo', 'bar'];
return inputs;
})
处理字符串时只需要花括号。正如@DimaGimburg所说,在你的ngModel中去掉它们。
<div class="texteditors">
<div class="form-group" ng-repeat="editor in app.inputs.editors">
<textarea class="form-control" name="{{$index}}" ng-model="inputs.editors[$index]" id="" cols="30" rows="10"></textarea>
</div>
</div>
ng-model
需要一个角度表达式。这意味着您不需要{{}}
表示法。
<textarea class="form-control" name="{{$index}}" ng-model="inputs.editors[$index]" id="" cols="30" rows="10"></textarea>
另一方面,name
并不期望角度表达式。这就是为什么你需要{{}}
。
相关文章:
- 如何将ng选项的索引作为angularJs中的值传递给模型
- Angular ng repeat order将多个字段作为一个字段
- 显示&作为&在jsp中使用angularjs而不使用ng-bind-html
- 当使用select作为标签时,如何使用ng选项禁用angularjs中的选项
- AngularJs的ng-click$事件将子元素作为目标传递
- 有没有一种方法可以使用angular中的$index作为ng重复中的模型
- 在 ng-include 中使用变量作为表达式
- Angular js指令使用控制器作为语法ng点击不起作用
- 从服务中获取ng patten作为变量
- 在调用AJAX之后显示ng控制器的内容;作为“;作用
- ng-repeat不能作为参数传递
- AngularJS:ng类,用于动态base64编码图像作为背景图像
- 如何在ng重复中的ng单击中将对象作为参数传递?AngularJS
- 使用模态ng控制器作为常规控制器
- 使用 NG 重复上的对象作为过滤器
- 用 jpeg/jpg 填充 ng-flow 作为 blob
- 获取 ng-show 值作为参考
- 在同一元素上使用“控制器作为 x”的多个 ng 控制器指令
- ng jsTree 返回错误的对象作为选定的节点
- Angular JS - 获取选定的文本作为ng模型(没有ng选项)