[英]How to use ng-repeat for array of objects and bind it to checkbox in angularjs
I am trying to display a checkbox inside Cards.我正在尝试在卡片中显示一个复选框。 So whenever the checkbox is checked the value is going to bind.
因此,只要选中复选框,该值就会绑定。 It is working fine.
它工作正常。 But I need to do this for many cards.
但我需要为许多卡执行此操作。 Instead of repeating the same code, I need to know the better way to achieve it.
我需要知道实现它的更好方法,而不是重复相同的代码。 I saw many of the solutions related to this but no use.
我看到了许多与此相关的解决方案,但没有用。
Controller code Controller代码
$scope.carBrand = {
"carbrands" : {
"BMW" : [
{
"d320" : $scope.bmwPermission.d320,
"note" : "Users who likes d320 ",
"configurable" : true
},
{
"d520" : $scope.bmwPermission.d520,
"note" : "Users who likes d520",
"configurable" : true
},
{
"d720" :$scope.bmwPermission.d720,
"note" : "Users who likes d720",
"configurable" : true
}
],
"AUDI" : [
{
"A1" : $scope.audiPermission.A1,
"note" : "Users who likes A1",
"configurable" : true
},
{
"A2" : $scope.audiPermission.A2,
"note" : "Users who likes A2",
"configurable" : true
},
{
"A3" : $scope.audiPermission.A3,
"note" : "Users who likes A3",
"configurable" : true
}
]
}
}
$scope.bmwDependencyCheck = function(newObj) {
if( $scope.bmwPermission.d320){
$scope.bmwPermission.d520 = true
$scope.bmwPermission.d720 = true
}else if ($scope.bmwPermission.d520){
$scope.bmwPermission.d720 = true
}
}
...
HTML CODE HTML 代码
<pre>
<div class="card inline col-md-3">
<div class="card-heading">
<div class="content-block lead inline col-md-12" >
<label class="col-md-8 text-align-left"> BMW </label>
</div>
</div>
<div class="card-body">
<ul class="flexview lbl-content">
<li class="lable col-md-10">
<label class="checkbox-inline" for="bmw-d320">
<input type="checkbox" name="chpview" id="bmw-d320"
ng-model="bmwPermission.d320"
ng-change="bmwDependencyCheck(bmwPermission.d320)" />d320
</label>
</li>
</ul>
<ul class="flexview lbl-content">
<li class="lable col-md-10">
<label class="checkbox-inline" for="bmw-d520">
<input type="checkbox" name="chpview" id="bmw-d520"
ng-model="bmwPermission.d520"
ng-change="bmwDependencyCheck(bmwPermission.d520)" />d520
</label>
</li>
</ul>
.
.
.
</div>
</div>
</pre>
I am not sure whether you meant this working model.我不确定你的意思是不是这个工作 model。 As said @Santhosh_mp you need to use two ng-repeat.
正如@Santhosh_mp 所说,您需要使用两个 ng-repeat。
Note, the value binding will be on unique keys...请注意,值绑定将在唯一键上...
in your case you need to create a model key for each and every row and check whether it exists, so the key reference is same on all loop在您的情况下,您需要为每一行创建一个 model 密钥并检查它是否存在,因此所有循环上的密钥引用都是相同的
{
"d320" : $scope.bmwPermission.d320,
"model": "d320"
"note" : "Users who likes d320 ",
"configurable" : true
},
<input ng-if="row.model === 'd320'" type="checkbox" ng-model="row.d320">
This method requires multiple checkboxes with ng-if.....and cannot work dynamic.. Say for example adding d321 will need to be coded again.此方法需要多个带有 ng-if 的复选框......并且不能动态工作。例如添加 d321 将需要再次编码。
So instead i changed the key to value instead of d320所以我改为将键改为值而不是 d320
I am not sure, why the ng-model for the second ng-repeat did not bind bind... So i made a seperate function and called it at initialize and on every model change我不确定,为什么第二个 ng-repeat 的 ng-model 没有绑定绑定...所以我做了一个单独的 function 并在初始化和每次 model 更改时调用它
<div ng-controller="CarsController">
<ul class="flexview lbl-content">
<li class="lable col-md-10">
<label class="checkbox-inline" for="bmw-d320">
<input type="checkbox" name="chpview" id="bmw-d320"
ng-model="bmwPermission.d320"
ng-change="bmwDependencyCheck(bmwPermission.d320)" />d320
</label>
</li>
</ul>
<ul class="flexview lbl-content">
<li class="lable col-md-10">
<label class="checkbox-inline" for="bmw-d520">
<input type="checkbox" name="chpview" id="bmw-d520"
ng-model="bmwPermission.d520"
ng-change="bmwDependencyCheck(bmwPermission.d520)" />d520
</label>
</li>
</ul>
<ul>
<li ng-repeat="(key, values) in carBrand.carbrands">{{key}}
<ul>
<li ng-repeat="row in values">
<input type="checkbox" ng-model="row.value">
{{row.note}}
{{row.configurable}}
</li>
</ul>
</li>
</ul>
</div>
JS JS
var app = angular.module('stackoverflow', []);
app.controller('CarsController', function($scope, $timeout){
$scope.bmwPermission={};
$scope.audiPermission={};
$scope.bmwPermission.d320 = false;
$scope.bmwPermission.d520 = false;
$scope.bmwPermission.d720 = false;
$scope.audiPermission.A1 = false;
$scope.audiPermission.A2 = false;
$scope.audiPermission.A3 = false;
$scope.defineCars = function(){
$scope.carBrand = {
"carbrands" : {
"BMW" : [
{
"value" : $scope.bmwPermission.d320,
"note" : "Users who likes d320 ",
"configurable" : true
},
{
"value" : $scope.bmwPermission.d520,
"note" : "Users who likes d520",
"configurable" : true
},
{
"value" :$scope.bmwPermission.d720,
"note" : "Users who likes d720",
"configurable" : true
}
],
"AUDI" : [
{
"value" : $scope.audiPermission.A1,
"note" : "Users who likes A1",
"configurable" : true
},
{
"value" : $scope.audiPermission.A2,
"note" : "Users who likes A2",
"configurable" : true
},
{
"value" : $scope.audiPermission.A3,
"note" : "Users who likes A3",
"configurable" : true
}
]
}
}
}
$scope.bmwDependencyCheck = function(newObj) {
if( $scope.bmwPermission.d320){
$scope.bmwPermission.d520 = true
$scope.bmwPermission.d720 = true
}else if ($scope.bmwPermission.d520){
$scope.bmwPermission.d720 = true
}
$scope.defineCars();
}
$scope.defineCars();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.