我有一个简单的模型数组(来自 rest API)
[{"applicationId":"64","createTime":"2012-07-13T14:56:06.395
07:00","createdByUserId":"s.s@s.com","lastActiveTime":"2012-08-31T13:45:49.869-
07:00","lastModifiedTime":"2012-07-13T14:56:06.395-
07:00","locationId":"16","locked":"1","scope":"1","stage":"STAGE_USER","status":"in
progress","useStatus":"N","userId":"dd.hello@gmai.com"}]
我在 html 页面中有一个复选框,选中后将仅显示状态为“已提交”的项目。所以我创建了一个手表如下:
$scope.$watch('showOnlySubmitted', function(newVal, oldVal){
if(newVal) {
angular.forEach($scope.applicationsCopy, function(app, index){
if(app.status && app.status!=="submitted"){
console.log("removed element at index: " + index);
console.log(app.applicationId + " : " + app.status);
$scope.applications.splice(index,1);
}
});
}
else {
$scope.applications = angular.copy($scope.applicationsCopy);
console.log("checkbox was un clicked !!");
}
});
如您所见,我制作了模型的副本,以便当用户取消选中过滤器时,我可以将原始模型重新复制回来。
.factory('applicationService', function($http) {
return {
getApplications: function(callback) {
$http.get('applications', {'8080': ':8080'}).success(callback);
}
}
})
和
$scope.applicationsCopy = angular.copy($scope.applications);
我可以在控制台中看到,如果项目状态不是“已提交”,则数组正在拼接,但拼接的项目在视图中仍然可见!。
视图绑定到 $scope.applications 如下:
<tr ng-repeat="app in applications">
<td>{{app.applicationId}}</td>
<td>{{app.projectName}}</td>
<td>{{app.createTime}}</td>
<td>{{app.status}}</td>
<td>{{app.createdByUserId}}</td>
</tr>
谢谢您的帮助 !。