/ / Liste der dynamischen Ankreuzfelder in Angular und JSON API - Json, Angularjs

Liste der dynamischen Kontrollkästchen in Angular und JSON API - json, angularjs

Ich habe Probleme beim Rendern dynamischer Kontrollkästchen mit JSON-API-Antwort.

Diese 2 ng-Wiederholungen:

  1. Bringt die Auflistung der Kategorien in BD und;
  2. ng-Modell mit der ausgewählten Kategorienliste.

Unter meinem HTML-Code;

<ul class="list-group">
<li class="list-group-item" ng-repeat="cats in categorias">
<div class="checkbox"><label><input type="checkbox" ng-model="checkbox[item.cats]"><span class="checkbox-material"><span class="check"></span></span></label> {{cats.name}}</div>
</li>
</ul>

JSON / API-Antwort (1)

[
{"id":"1","id_module":"1","name":"Esportes"},
{"id":"2","id_module":"1","name":"Entretenimento"},
{"id":"3","id_module":"1","name":"Terror"},
{"id":"4","id_module":"1","name":"Drama"}
]

JSON-Antwort (2)

{cats":["1","2"]}

Ich möchte, dass das Kontrollkästchen mit der Antwort überprüft wird.

Hat jemand eine Idee?

Antworten:

1 für die Antwort № 1

Hier haben Sie Geige arbeiten, überprüfen Sie es

jsfiddle.net/b895j3ay

var app = angular.module("Application", [])
app.controller("Ctrl", function($scope) {
$scope.roles = [{
id: 1,
text: "guest"
}, {
id: 2,
text: "user"
}, {
id: 3,
text: "customer"
}, {
id: 4,
text: "admin"
}];
$scope.isChecked = function(id, matches) {
var isChecked = false;
angular.forEach(matches, function(match) {
if (match === id) {
isChecked = true;
}
});
return isChecked;
}
$scope.user = {
roles: [2, 4, 3]
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js"></script>
<div ng-app="Application">
<div ng-controller="Ctrl">
<label ng-repeat="role in roles">
<input type="checkbox" ng-model="user.roles" checklist-value="role.id" ng-checked="isChecked(role.id,user.roles)">{{role.text}}
</label>
</div>
</div>
</div>