我想在Angular JS应用中设置一些过滤器复选框。我已尝试将ng-init
与checked=true
一起使用,但它无法正常工作。我想将所有复选框设置为最初检查。
http://plnkr.co/edit/AtFGIyoo0h15XMl63aXK?p=preview
我对此很新,但我一直试图找到我追求的信息。所以,现在我确定我需要通过设置模型内部的值来设置'检查'。所以(这是我完全错误的地方)我正在使用:
$scope.location = ['rural', 'urban', 'coastal']
使用如下复选框:
<label><input type="checkbox" ng-model="location.rural" class="ng-valid ng-dirty">Rural</label>
<label><input type="checkbox" ng-model="location.urban" class="ng-pristine ng-valid">Urban</label>
<label><input type="checkbox" ng-model="location.coastal" class="ng-pristine ng-valid">Coastal</label>
<hr>
<label><input type="checkbox" ng-model="property_type.pub" class="ng-valid ng-dirty">pub</label>
<label><input type="checkbox" ng-model="property_type.bandb" class="ng-valid ng-dirty">bandb</label>
<label><input type="checkbox" ng-model="property_type.hotel" class="ng-valid ng-dirty">hotel</label>
这是在上下文中:
propertyApp.controller('PhoneListCtrl', function ($scope) {
$scope.properties = [
{
title: "Sharrow Bay Hotel",
location:['rural', 'coastal'],
property_type: ['pub']
},
{
title: "The Royal Oak Inn",
location:['rural'],
property_type: ['pub']
},
{
title: "Scale Hill Cottages",
location:['urban'],
property_type: ['hotel']
},
];
$scope.location = ['rural', 'urban', 'coastal']
$scope.title = "Phone list";
$scope.multiplier = 3;
$scope.isEmpty = function (obj) {
return angular.equals({},obj);
};
})
如何在init上选中复选框?
答案 0 :(得分:1)
从 ng-model 指令开始,应该引用模型中的bool而不是字符串。然后,您可以在控制器中将这些bool初始化为true或false:
propertyApp.controller('PhoneListCtrl', function ($scope) {
$scope.location = {
rural: true,
urban: true,
coastal: true
}
$scope.property_type = {
pub: true,
bandb: true,
hotel: true
}
});
也让角度为你准备出原始和肮脏的东西。