需要对角度和复选框提供一些帮助,以及如何将一个变量与所有选中的值组合在一起。这是我到目前为止得到的但似乎无法找到一个好的连接所有检查的值的方法,以便它们可以运送到REStful $资源。我如何获得一些变量或$ scope项,其中包含所有选中的值,格式如下。 e.g:
types = Permanent,Contract,Any,Fixed Term
复选框的示例数据($ scope.newJobSearch.type):
{
"Permanent": "Permanent",
"Contract": "Contract",
"Permanent or Contract": false,
"Any": "Any",
"Fixed Term": "Fixed Term"
}
<label class="checkbox-inline" for="{{jobType}}" ng-repeat="jobType in jobTypes ">
<input type="checkbox" id="{{jobType}}" value="{{jobType}}" ng-model="newJobSearch.type[jobType]" ng-true-value="{{jobType}}" ng-click="jobTypesCheck()"> {{jobType}}
</label>
$scope.newJobSearch = {};
function searchJobs(newJobSearch) {
angular.forEach(newJobSearch.type, function (value) {
if (value != false) {
$scope.queryTypes.push(value); //Dose not seem to work
//Some var in here which contains all the string values e.g:
//Permanent,Contract,Fixed Term
}
});
}
答案 0 :(得分:1)
查看 jsFiddle
您可以使用ng-repeat
和ng-change
的组合来更新结果集:
控制器:
$scope.result = {};
$scope.items = [
{ name: "Permanent", checked: false },
{ name: "Contract", checked: false },
{ name: "Permanent or Contract", checked: false },
{ name: "Any", checked: false },
{ name: "Fixed", checked: false }
];
$scope.updateResult = function () {
$scope.items.forEach(function (item) {
$scope.result[item.name] = item.checked ? item.name : false;
});
};
$scope.updateResult();
html:
<div ng-repeat="item in items">
<input ng-model="item.checked"
ng-change="updateResult()"
type="checkbox">
{{ item.name }}
</div>
答案 1 :(得分:0)
我根本不懂你的代码。无论如何,我认为你正在寻找类似的东西:
$scope.jobTypes = ['Permanent','Contract','Any','Fixed Term'];
$scope.newJobSearch;
$scope.newJobSearch.type = [];
$scope.newJobSearch.type.checked = $filter('filter')($scope.newJobSearch.type, {checked: true});
然后:
<label class="checkbox-inline" for="{{jobType}}" ng-repeat="jobType in jobTypes ">
<input type="checkbox" id="{{jobType}}" value="{{jobType}}" ng-model="newJobSearch.type[$index]" > {{jobType}}
</label>
您不需要将数据绑定到模型的函数,因为Angular会自行完成。