【发布时间】:2014-02-21 01:28:23
【问题描述】:
在输入标签类型复选框中将 ng-models 与 ng-repeat 绑定时遇到问题。 我先附上我的代码,然后再详细解释。
app/main.html:
<div ng-repeat="feature in features">
<input type="checkbox" ng-model="features[$index].name">{{features[$index].name}}
</div>
<br></br>
<div class="highlighter">
<span ng-class="{emo:Emotions}">Manually</span> <span ng-class="{feel:Feelings}">create</span> the <span ng-class="{emo:Emotions}">entire</span>
</div>
main.js
angular.module('webClientApp')
.controller('MainCtrl', function ($scope,$http) {
[...other variables...]
$scope.features = [{'name':'Emotions'},{'name':'Feelings'}];
[...other parts of code]
});
我们还假设在 main.css 文件中分别引用了 .emo' and.feel' 类,以在用户勾选与该功能相关的框时突出显示目标词。
现在,当我一一列出所有输入时,应用程序可以正常工作,如下所示:
<input type="checkbox" ng-model="Emotions">Emotions
<input type="checkbox" ng-model="Feelings">Feelings
但我想将它包装成一个 ng-repeat 并列出控制器范围内的功能,因为我将考虑的功能会更多。当我在框上打勾时尝试上面的代码时,名称更改为“true”。
我已经阅读了很多关于如何将模型绑定到输入标签内的 ng-repeat,但没有一个解决方案适用于我的案例。 有人可以帮帮我吗?
【问题讨论】:
-
ng-model 需要设置为您想要“true”或“false”的属性,以判断该框是否被选中(或自定义 true 或 false 值)。不要在 ng-model 上设置你的名字。查看文档。 docs.angularjs.org/api/ng/input/input%5Bcheckbox%5D 尝试将 ng-model 设置为 features[$index].selected。
标签: javascript html angularjs checkbox angular-ngmodel