如何使用$ scope。$ watchCollection insted of angular.forEach()

时间:2015-07-20 18:50:09

标签: javascript angularjs foreach

当用户执行删除或取消删除时,我有这段代码,它将更新模型上的已删除属性。我的问题我用于angular.forEach()只是想看看是否有可能改变angular.forEach以使用$ scope。$ watchCollection()?

    $scope.deleteForm = function (form) {
        var result = confirm('Want to delete the form ?');
        if (result){
            var splitDeleteHref = form.deleteHref.split('/');
            var formName = splitDeleteHref[1];
            var formId = splitDeleteHref[2];
            homeService.deleteForm(formName, formId);
                $scope.$watchCollection('forms',)
            //angular.forEach($scope.forms, function(value, key) {
            //    if (value.recordId === form.recordId) {
            //        $scope.forms[key].deleted = true;
            //    }
            //});
        }
    };

    $scope.undeleteForm = function (form) {
        var result = confirm('Want to undelete the form ?');
        if (result) {
            homeService.undeleteForm(form).then(function () {
                //angular.forEach($scope.forms, function (value, key) {
                //    if (value.recordId === form.recordId) {
                //        $scope.forms[key].deleted = false;
                //    }
                //});
            });

        }
    };

1 个答案:

答案 0 :(得分:0)

$scope.$watch.$watchCollection用于观察对象或对象集合的更改。这些方法不能用于迭代数组,这是angular.forEach的用途。您对.forEach的使用似乎是正确的,因此我建议保持原样。