在我的场景中,我有一个选择下拉列表,其中分配了一个ng-model。下拉列表中的每个项目都对应于在进行选择时选中的多个复选框。困难在于,我不能只检查一个简单的key:value,因为复选框不是特定选择所独有的。例如:
<select>
<option>Dog</option>
<option>Cat</option>
<option>Bird</option>
</select>
<input type="checkbox">Is an animal
<input type="checkbox">Can bark
<input type="checkbox">Can meow
<input type="checkbox">Can tweet
<input type="checkbox">Has four legs
<input type="checkbox">Has wings
因此,您可以不排除“是动物”和“具有四只脚”的复选框。我当前的实现使用一个简单的条件表达式来评估是否标记了该复选框(ng-checked =“ animal =='dog'”),但是当然,这排除了其他两种可能性。所以我想知道是否存在本机Angular处理OR语句或数组的方式。否则,我该如何使用JavaScript或jQuery做到这一点?
看到这个小提琴:http : //jsfiddle.net/QHza7/
模板:
<div ng-app ng-controller="Test">
<select ng-model="opts" ng-options="a.opts as a.name for a in answers"></select>
<br/>
<input type="checkbox" ng-model="opts.animal" />Is an animal<br/>
<input type="checkbox" ng-model="opts.bark" />Can bark<br/>
<input type="checkbox" ng-model="opts.meow" />Can meow<br/>
<input type="checkbox" ng-model="opts.tweet" />Can tweet<br/>
<input type="checkbox" ng-model="opts.legs4" />Has four legs<br/>
<input type="checkbox" ng-model="opts.wings" />Has wings<br/>
</div>
码:
function Test($scope) {
$scope.answers = [
{name:"Dog", opts:{animal:true,bark:true,legs4:true}},
{name:"Cat", opts:{animal:true,meow:true,legs4:true}},
{name:"Bird", opts:{animal:true,tweet:true,wings:true}}
];
$scope.opts = null;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句