重置更改的值在其他Ctrl(AngularJS)中不起作用

时间:2015-06-11 12:29:19

标签: javascript angularjs controller reset

我对我的问题有点困惑。事实上,我有两个使用服务的视图和ctrl。 First View包含一个表列表,其中包含将从WebAPI加载的项目。该服务向服务器发出请求并提供订购。此外,我正在使用其他服务在另一个Ctrl中传输所选项目行。 这是代码:

视图1:

//view1.html
<table class="table table-bordered table-hover">
  <thead>
    <tr>
      <th>Firstname</th>
      <th>Lastname</th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="item in namelist" ng-click="open(this.item)">
      <td>{{ item.fname }}</td>
      <td>{{ item.lname }}</td>
    </tr>
  </tbody>
</table>

CTRL1:

//FirstCtrl
$scope.namelist = reqService.names.query();

$scope.open = function (item) {
  $scope.selectedItem = item;           
  modalService.openDialog($scope.namelist, $scope.selectedItem);
}

HTTP-服务:

//Service for HTTP Requests
testApp.factory('reqService', ['$resource', 'baseUrl', function ($resource, baseUrl) {
    return {
        names: $resource(baseUrl + '/api/name/:Id', {
            Id: '@Id'
        }, {
            'update': {
                method: 'PUT'
            }
        })
    }
}]);

模态对话框的服务:

//Modal dialog service
testApp.factory('modalService', ['$modal', function ($modal) {
    return {
        openDialog: function (namelist, selectedItem) {
            return $modal.open({
                templateUrl: 'views/view2.html',
                controller: 'SecondCtrl',
                resolve: {
                    namedList: function () {
                        return namelist;
                    },
                    selected: function () {
                        return selectedItem;
                    }
                }
            });
        }
    }
}]);

CTRL2:

testApp.controller('SecondCtrl', ['$scope', '$modalInstance', 'namedList', 'selected', 'reqService', '$http'..., function (...){
   /*copy of the original items*/
   $scope.copyItem = angular.copy(selected);

   $scope.cancel = function () {
      $scope.selected = angular.copy($scope.copyItem);
      $modalInstance.dismiss('cancel');
   }

   $scope.reset = function () {
      $scope.selected = angular.copy($scope.copyItem);
      selected = angular.copy($scope.copyItem); //doesn't work
   }
}

我的问题是如何重置表格列表?当我点击resetBtn时,它只重置我的模态窗口中的表格,但更改仍保留在表格列表中?!我无法重置“已选择”的解析变量。

1 个答案:

答案 0 :(得分:0)

可以通过值参考传递一个案例 阅读此Is JavaScript a pass-by-reference or pass-by-value language?

如果你尝试传递一个重置值的函数,那将会很有趣。

一种可能的实施方式

CTRL1:

$scope.open = function (item) {
    $scope.selectedItem = item;           
    modalService.openDialog($scope.namelist, function(e){
        if (e === undefined) {
            return $scope.selectedItem;
        } else {
            $scope.selectedItem = e;
        }
    });
}

Ctrl2开始:

testApp.controller('SecondCtrl', ['$scope', '$modalInstance', 'namedList', 'selected', 'reqService', '$http'..., function (...){
    /*copy of the original items*/
   $scope.copyItem = angular.copy(selected());

   $scope.cancel = function () {
      $scope.selected = angular.copy($scope.copyItem);
      $modalInstance.dismiss('cancel');
   }

   $scope.reset = function () {
      $scope.selected = angular.copy($scope.copyItem);
      selected(angular.copy($scope.copyItem));
   }
}

好的,我知道它很脏但只是为了测试这个假设。