我正在为我的一个项目使用angularjs。我正在尝试实现过滤功能。
我正在使用下面的代码构建过滤器复选框
<div class="checkbox" ng-repeat="incidentType in keywords.incidentType">
<label><input type="checkbox" value={{incidentType}}>{{incidentType}}</label>
</div>
结果如下图所示
在选中或取消选中复选框时,我需要创建一个如下所示的对象。基于此json对象,我将向服务器发送请求以获取匹配数据。
{
"application": [
"Athena",
"EWS"
],
"incidentType": [
"Publishing Failure",
"Security Failure"
]
}
关于如何在angularjs中实现这一点的任何想法。非常感谢任何帮助。
答案 0 :(得分:3)
对于类似的东西,你可以使用像你想要发送的对象作为模型,然后将它绑定到ng-repeat中的数据。我先让你在这里查看:http://jsfiddle.net/DotDotDot/gcEJH/
我已经采用了您的示例代码,我刚刚添加了一个带有对象($scope.checked
)的控制器,我在每个复选框中使用了ng-model
$scope.checked={application:{}, incidentType:{}};
然后,在HTML
中<div class="checkbox" ng-repeat="incidentType in keywords.incidentType">
<label><input type="checkbox" ng-model='checked.incidentType[incidentType]' ng-true- value='{{incidentType}}'>{{incidentType}}</label>
</div>
ng-model部分告诉angular将值放在对象的incidentType部分中,在与该值对应的kay下。这不会给你完全相同的对象,但你会有类似的东西:
{
"application": {
"Athena": false,
"EWS": true,
"EWindows": true
},
"incidentType": {
"Publishing Failure": true,
"Security Failure": true
}
}
实际上非常接近,您可以从中创建请求(或者轻松地重新创建您想要的对象)
希望这有帮助,玩得开心
=)