简体   繁体   English

有没有办法在ng-repeat中使用angular中的$ index作为模型?

[英]Is there a way to use $index in angular as the model in an ng-repeat?

I have a factory that I am pushing values to from my controller my controller. 我有一个工厂,正在将值从控制器推送到控制器。

.factory("Inputs", function(){

    var inputs = {

    };

    inputs.editors = [0];

    return inputs;

})

I want to use $index as the model when I loop through it like so 我想像这样循环遍历时使用$ 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>

but it won't evaluate, the only way I was able to get it to evaluate is like this: 但它不会评估,我唯一能够评估的方法是这样的:

but now its a string it evaluates to inputs.editors['0'] 但现在它是一个字符串,它的计算结果为inputs.editors ['0']

I want to then loop through the ng-models and evaluate them {{inputs.editors[0]}} to how ever many text areas are added. 然后,我想遍历ng模型并评估它们{{inputs.editors [0]}}以了解添加了多少文本区域。 I am not sure if I explained properly. 我不确定我是否解释正确。

Should I use directive to create the binding, how would I be able to evaluate the textareas and models that are being generated from pushing the values to the factory? 我是否应该使用指令来创建绑定,我如何能够评估通过将值推送到工厂而生成的文本区域和模型?

You only need the curly brackets when you are dealing with strings. 处理字符串时,只需要大括号。 As @DimaGimburg also said, get rid of them in your ngModel. 就像@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>

Here you go: 干得好:

http://codepen.io/jlowcs/pen/ogJBVm http://codepen.io/jlowcs/pen/ogJBVm

A few things were wrong with your markup. 您的标记有些错误。

You also need to add track by to your ng-repeat , otherwise you will lose focus every time the model changes because the textarea is recreated by the ng-repeat . 您还需要向ng-repeat添加track by ,否则每次模型更改时都会失去焦点,因为ng-repeat会重新创建文本区域。

HTML: 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: JS:

app.controller('MyCtrl', function($scope, Inputs) {
    this.inputs = Inputs;
})
.factory("Inputs", function(){
    var inputs = {};

    inputs.editors = ['foo', 'bar'];

    return inputs;

})

ng-model expects an angular expression. ng-model期望一个角度表达式。 That means that you don't need the {{}} notation. 这意味着您不需要{{}}表示法。

<textarea class="form-control" name="{{$index}}" ng-model="inputs.editors[$index]" id="" cols="30" rows="10"></textarea>

On the other hand, name doesn't expect an angular expression. 另一方面, name不期望使用角度表达式。 Thats why you needed the {{}} there. 因此,您需要在那里使用{{}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM