[英]AngularJS: how to generate dynamic ng-model for input type [text]
My current scenario is: I've doing nesting repetition like follow: 我当前的情况是:我正在执行嵌套重复,如下所示:
<form name="task_form" ng-app="myApp" ng-submit="tasksubmit()">
<ul class="items-list">
<li ng-repeat="task in taskslist | orderBy:orderProp">
<p>
<strong>{{task.title}}</strong>
</p>
<input type="text" ng-model="task_input_values[task.id]" >
</li>
</ul>
</form>
If in tasklist
array i have 100+ tasks
then it means i have more then 100 same ng-model
values for <input type=text>
. 如果在
tasklist
数组中我有100多个tasks
那么这意味着我有<input type=text>
超过100个相同的ng-model
值。 Here im using ng-model="task_input_values[task.id]"
it give me the array like {"14":"sometext here"}
i dont know how to capture the id
that is 14
and input value
that is sometext here
. 在这里,我使用
ng-model="task_input_values[task.id]"
它给了我{"14":"sometext here"}
这样的数组,我不知道如何捕获id
为14
input value
和sometext here
input value
。 Moreover, if u have any better logic please help me. 此外,如果您有更好的逻辑,请帮助我。 I'm newbie in angularJS.
我是angularJS的新手。
You can edit directely the object you are iterate : 您可以直接编辑要迭代的对象:
<form name="task_form" ng-app="myApp" ng-submit="tasksubmit()">
<ul class="items-list">
<li ng-repeat="task in taskslist | orderBy:orderProp">
<p>
<strong>{{task.title}}</strong>
</p>
<input type="text" ng-model="task.title" >
</li>
</ul>
</form>
See live demo: live demo You just need to use your tasks
variable as an array of objects: 请参阅实时演示: 实时演示您只需要将
tasks
变量用作对象数组:
$scope.taskslist = [
{title: "I am first text input"},
{title: "I am second text input"}
];
and then bind to the value property of the object in the ngRepeat: 然后绑定到ngRepeat中对象的value属性:
<form name="task_form" ng-app="myApp" ng-submit="tasksubmit()">
<ul class="items-list">
<li ng-repeat="task in taskslist | orderBy:orderProp">
<p>
<strong>{{task.title}}</strong>
</p>
<input type="text" ng-model="task.title" >
</li>
</ul>
</form>
Since tasks
is an array - you don't need to specify the id as tasks[$index]
give you the value of the title ;) Ex: tasks[1]
gives you: {text: "I am second text input"}
由于
tasks
是一个数组-您无需指定id,因为tasks[$index]
为您提供标题的值;)例如: tasks[1]
会为您提供: {text: "I am second text input"}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.