gpt4 book ai didi

angularjs - Angular.JS : why can't the inputs be edited?

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

这是一个奇怪的问题。代码很简单:

HTML代码:

<body ng-controller="MainCtrl">
<ul ng-repeat="name in names">
<input type="text" ng-model="name" />
</ul>
</body>

Angular 代码:
app.controller('MainCtrl', function($scope) {
$scope.names = ["aaa","bbb","ccc"];
});

实时演示URL为: http://plnkr.co/edit/2QFgRooeFUTgJOo223k9?p=preview

我不明白为什么不能编辑输入控件,我不能键入新字符或删除字符。

最佳答案

由于范围继承,这是一个常见问题。您的每个names都是原始类型,因此ng-repeat使其成为未与原始对象连接的自己的范围项目,但是,如果每个names是一个对象,则ng-repeat范围项目将是对原始对象的引用

 [{name:"aaa"},{name:"bbb"},{name:"ccc"}];

始终在 dot中使用 ng-model是有用的经验法则
<div ng-repeat="item in names">
<input type="text" ng-model="item.name"/>
</div>

Working Plunker

请阅读Angular github Wiki上的这篇文章,以了解详细的解释:

https://github.com/angular/angular.js/wiki/The-Nuances-of-Scope-Prototypal-Inheritance

关于angularjs - Angular.JS : why can't the inputs be edited?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15720334/

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