ng-repeat中的角度js - ng模型

时间:2022-12-02 16:41:34

Different 'ng-model name' in ng repeat - Possible?

ng重复中的'ng-model name'不同 - 可能吗?

<div  ng-repeat="todo in todos">
   <input type="text" ng-model="tag">  
   <button type="submit"ng-click="addTodo(todo._id)">Add</button>
</div>

In this case, there are some repeat todo item (based on todos json data) will show on frontend

在这种情况下,有一些重复todo项目(基于todos json数据)将在前端显示

My Problem: What i type on any input field , all input field showing same data

我的问题:我在任何输入字段上键入的内容,所有输入字段显示相同的数据

I need different ng-model name on each input field , I guess like this ng-model="tag($index)"

我需要在每个输入字段上使用不同的ng-model名称,我想这样的ng-model =“tag($ index)”

3 个解决方案

#1


2  

You could place the newly created model inside tag array by its $index, while declaring model inside the tag you should use array notation [] instead of ()

您可以通过其$ index将新创建的模型放在标记数组中,同时在标记内声明模型,您应该使用数组表示法[]而不是()

ng-model="tag($index)" 

should be

应该

ng-model="tag[$index]"

Markup

标记

<div  ng-repeat="todo in todos">
   <input type="text" ng-model="tag[$index]">  
   <button type="submit"ng-click="addTodo(todo._id)">Add</button>
</div>

#2


2  

It is possible like below

有可能如下

<div  ng-repeat="todo in todos">
   <input type="text" ng-model="tag[todo]">  <--todo.key-->
   <button type="submit"ng-click="addTodo(todo._id)">Add</button>
</div>

#3


1  

You can do it like this:

你可以这样做:

 <input type="text" ng-model="todo.tag">  

#1


2  

You could place the newly created model inside tag array by its $index, while declaring model inside the tag you should use array notation [] instead of ()

您可以通过其$ index将新创建的模型放在标记数组中,同时在标记内声明模型,您应该使用数组表示法[]而不是()

ng-model="tag($index)" 

should be

应该

ng-model="tag[$index]"

Markup

标记

<div  ng-repeat="todo in todos">
   <input type="text" ng-model="tag[$index]">  
   <button type="submit"ng-click="addTodo(todo._id)">Add</button>
</div>

#2


2  

It is possible like below

有可能如下

<div  ng-repeat="todo in todos">
   <input type="text" ng-model="tag[todo]">  <--todo.key-->
   <button type="submit"ng-click="addTodo(todo._id)">Add</button>
</div>

#3


1  

You can do it like this:

你可以这样做:

 <input type="text" ng-model="todo.tag">