我有一個服務,比如說this one返回angularJS,壓平復選框列表到CSV
{"001":"Communication","002":"Developement","003":"Environment","004":"Equipment"}
我需要把這個所有的複選框,用戶啓用,禁用它們,最後我養傷的檢查值CSV鍵。
說用戶檢查了「開發」和「設備」,所以我需要獲得「002,004」的值。
這裏是my codepen與已經檢查(002-發展論壇及003-環境)的一些值:
angular.module('tagsApp', [])
.controller('tagsController', ['$scope', '$http', function ($scope, $http) {
// an initial value is present in the #Tags hidden element
$scope.tags = $('#Tags').val();
var tags = $scope.tags.split(",");
// I need an obj['key']='key' array
$scope.myTagsArray = {};
tags.forEach(function (tag) { $scope.myTagsArray[tag] = tag; });
// get all possible values
$http.get("http://www.mocky.io/v2/597866a7130000d704c0fed3")
.then(function (response) {
$scope.allTags = response.data;
});
$scope.change = function (myTagsArray) {
console.log("myTagsArray: '" + Object.values($scope.myTagsArray).join(",") + "' !");
};
}]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="tagsApp">
<label>Tags</label>
<div ng-controller="tagsController">
<input type="hidden" id="Tags" value="002,003"/>
<div ng-cloak ng-repeat="(key, value) in allTags">
<label for="tag_{{key}}">
<input type="checkbox"
id="tag_{{key}}"
ng-model="tagsArray['{{key}}']"
ng-true-value="'{{key}}'"
ng-false-value=""
ng-change="change(tagsArray)" />
{{value}}
</label>
</div>
</div>
</div>
但是所有的代碼並未真正發揮作用。哪裏有問題?
您的ng模型不需要{{}}。像這樣改變它ng-model =「tagsArray [key']」 ng-true-value =「'key'」 – Vivz
謝謝,好點,不幸的是這並沒有解決問題... – Serge
複選框在哪裏你的codepen? – Vivz