【发布时间】:2016-08-09 05:40:57
【问题描述】:
TLDR:这是问题的 plnkr:https://plnkr.co/edit/HfRoCgPfdoZNxmTtDLf7?p=preview
我有一个带有复选框的表单:
<div class="form-group">
<div class="input-group">
<div class="checkbox">
<label>
<input type="checkbox" ng-model="rental.car2go.airport.berlin"> Berlin
</label>
<br/>
<label>
<input type="checkbox" ng-model="rental.car2go.airport.hamburg"> Hamburg
</label>
<br/>
</div>
</div>
</div>
即修改出租对象。这是控制器的样子:
angular.module('c2gyoApp', []).controller('c2gyoAppController', [
'$scope',
'state',
function($scope, state) {
$scope.airports = [{
"name": "Berlin",
"model": "rental.car2go.airport.berlin"
}, {
"name": "Hamburg",
"model": "rental.car2go.airport.hamburg"
}, ];
$scope.rental = state.rental;
}]).factory('state', function() {
var rental = {
car2go: {
airport: {
berlin: false,
hamburg: false
}
}
};
return {
rental: rental
};
});
现在我想使用 ng-repeat:
<div class="form-group">
<div class="input-group">
<div class="checkbox">
<span ng-repeat="airport in airports">
<label>
<input type="checkbox"
ng-model="airport.model">
{{airport.name}}
</label>
<br>
</span>
</div>
</div>
</div>
使用 ng-repeat 表单正在修改 airports 对象(airports[1].model=true/false 和 airports[2].model=true/false)。
它应该只使用该机场对象的字符串(rental.car2go.airport.berlin 和rental.car2go.airport.hamburg)并修改出租对象。我正在寻找一种将字符串传递给 ng-model 的方法,而不是 airports 对象。我该怎么做?
编辑:删除指令,新的 plnkr
【问题讨论】:
-
不清楚您为什么声称它无法正常工作。复选框的 值 是
true或false,因此替换如您所见(在两个示例中)。 -
它可能工作正常,但不是我想要的 ;) 检查 plunkr (plnkr.co/edit/HfRoCgPfdoZNxmTtDLf7?p=preview) 我想修改一个不同的对象(rental.car2go.airport.xxx)而不是那个被传递给 ng-repeat(机场)。
-
angular 不支持这种绑定
ng-model的方式。我尝试了一些选项,例如使用ng-modelOptionsgetter/setter,但找不到有效的选项。您可能需要执行一些操作,例如将复选框绑定到airport上的属性,然后将值复制到ng-click函数中的rental数组。
标签: angularjs angularjs-ng-repeat angular-ngmodel