【发布时间】:2014-05-08 14:21:48
【问题描述】:
我是 angularjs 新手
HTML 文件:
<div class='col-md-2 searchdisplay-col1' ng-controller="amenitiesController" style="margin-top:20px">
<h4>Amenities</h4>
<label ng-repeat="facilityName in facilityNames" style="display:block;">
<input
type="checkbox"
name="{{facilityName.name}}"
value="{{facilityName.id}}"
ng-checked=""
ng-click="lodgeFilter($event,facilityName.id)"
/> {{facilityName.name}}
</label>
</div>
控制器:
App.controller('amenitiesController',['$scope','$http', function($scope,$http){
$scope.facilityNames = []; // Initialize array for checkboxes
$http({method: 'GET', url: "/api/facilities.json"})
.success(function(data, status) {
$.each(data, function(i,f) {
$scope.facilityNames.push({ name: f.facility.name, id: f.facility.id });
});
})
.error(function(data, status) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
$scope.lodgeFilter = function($event, id) {
html = "";
$http({method: 'GET', url: "/api/location/"+location_id+".json"})
.success(function(data, status) {
//Some Code
})
.error(function(data, status) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
$("#results-display").html(html);
}
}]);
Json 示例
[
{
"facility": {
"id": 1,
"name": "Internet"
}
},
{
"facility": {
"id": 2,
"name": "Bar"
}
}
]
现在当我点击复选框时,我得到了预期的结果。但是当我取消选中它仍然处于相同状态时,我如何让它进入初始状态?
【问题讨论】:
-
使用
ng-model+ng-true-value&ng-false-value将一些数据绑定到您的复选框,并在您的lodgeFilter函数中观察该特定模型属性。通过这种方式,您将能够检测复选框是否已被勾选并采取相应措施。 问你一个问题:你对 DHTML(Javascript + HTML)编程也很陌生吗? -
我如何绑定每个复选框,因为它在一个循环中?是的,我对(javascript + Html)很陌生
-
我无法提供 plunker 代码,因为复选框是从本地 json 数据生成的,并且它没有托管在任何服务器上。
标签: javascript jquery ajax angularjs checkbox