gpt4 book ai didi

angularjs - ng-model、ng-repeat 和输入方面的困难

转载 作者:行者123 更新时间:2023-12-03 04:38:02 27 4
gpt4 key购买 nike

我试图允许用户使用 ngRepeatngModel 编辑项目列表。 ( See this fiddle 。)但是,我尝试过的两种方法都会导致奇怪的行为:一种方法不会更新模型,另一种方法会在每次按键时模糊表单。

我在这里做错了什么吗?这不是受支持的用例吗?

这是 fiddle 中的代码,为方便起见复制:

<html ng-app>
<head>
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.2.1/css/bootstrap-combined.min.css" rel="stylesheet">
</head>
<body ng-init="names = ['Sam', 'Harry', 'Sally']">
<h1>Fun with Fields and ngModel</h1>
<p>names: {{names}}</p>
<h3>Binding to each element directly:</h3>
<div ng-repeat="name in names">
Value: {{name}}
<input ng-model="name">
</div>
<p class="muted">The binding does not appear to be working: the value in the model is not changed.</p>
<h3>Indexing into the array:</h3>
<div ng-repeat="name in names">
Value: {{names[$index]}}
<input ng-model="names[$index]">
</div>
<p class="muted">Type one character, and the input field loses focus. However, the binding appears to be working correctly.</p>
</body>
</html>

最佳答案

这似乎是一个具有约束力的问题。

建议是don't bind to primitives .

您的 ngRepeat 正在迭代集合内的字符串,而它应该迭代对象。解决您的问题

<body ng-init="models = [{name:'Sam'},{name:'Harry'},{name:'Sally'}]">
<h1>Fun with Fields and ngModel</h1>
<p>names: {{models}}</p>
<h3>Binding to each element directly:</h3>
<div ng-repeat="model in models">
Value: {{model.name}}
<input ng-model="model.name">
</div>

jsfiddle:http://jsfiddle.net/jaimem/rnw3u/5/

关于angularjs - ng-model、ng-repeat 和输入方面的困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13714884/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com