我有一个ng-repeat,我希望将重复的值用作数组索引值。
控制器:
vm.ingredientsCount = [0,1,2];
模板
<div ng-repeat="count in vm.ingredientsCount">
<h3>Ingredients {{count}} {{$index}}</h3>
<formly-form model="vm.model.ingredients[$index]" fields="vm.fields.ingredients[$index]"> </formly-form>
<h4>Ingredients Quantity</h4>
<formly-form model="vm.model.ingredients[$index].quantity" fields="vm.fields.ingredients[$index].quantity"> </formly-form>
</div>
在呈现的HTML中{{count}}
和{{index}}
被正确插值,但任何[$index]
或[{{$index}}]
都不会进行插值。
呈现HTML
<div ng-repeat="count in vm.ingredientsCount">
<h3>Ingredients 0 0</h3>
<formly-form model="vm.model.ingredients[$index]" fields="vm.fields.ingredients[$index]"> </formly-form>
<h4>Ingredients Quantity</h4>
<formly-form model="vm.model.ingredients[$index].quantity" fields="vm.fields.ingredients[$index].quantity"> </formly-form>
</div>
<div ng-repeat="count in vm.ingredientsCount">
<h3>Ingredients 1 1</h3>
<formly-form model="vm.model.ingredients[$index]" fields="vm.fields.ingredients[$index]"> </formly-form>
<h4>Ingredients Quantity</h4>
<formly-form model="vm.model.ingredients[$index].quantity" fields="vm.fields.ingredients[$index].quantity"> </formly-form>
</div>
<div ng-repeat="count in vm.ingredientsCount">
<h3>Ingredients 2 2</h3>
<formly-form model="vm.model.ingredients[$index]" fields="vm.fields.ingredients[$index]"> </formly-form>
<h4>Ingredients Quantity</h4>
<formly-form model="vm.model.ingredients[$index].quantity" fields="vm.fields.ingredients[$index].quantity"> </formly-form>
</div>
答案 0 :(得分:0)
不确定表格形式是做什么的,但是根据你最后评论的$index
被吐出来而[{{$index}}]
无效。解决方案是改为:
<div ng-repeat="count in vm.ingredientsCount">
<h3>Ingredients {{count}} {{$index}}</h3>
<formly-form model="{{ vm.model.ingredients[$index] }}" fields="{{ vm.fields.ingredients[$index] }}"> </formly-form>
<h4>Ingredients Quantity</h4>
<formly-form model="{{ vm.model.ingredients[$index].quantity }}" fields="{{ vm.fields.ingredients[$index].quantity }}"> </formly-form>
</div>