【问题标题】:Reset the changed values doesn't work in other Ctrl (AngularJS)重置更改的值在其他 Ctrl (AngularJS) 中不起作用
【发布时间】:2015-06-11 12:29:18
【问题描述】:

我对我的问题有点困惑。事实上,我有 2 个视图和 ctrl 正在使用服务。 第一个视图包含一个表格列表,其中包含将从 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 时,它仅重置我的模式窗口中的表单,但更改仍保留在表格列表中?!我无法重置解析变量“已选择”。

【问题讨论】:

  • 有趣的问题,您可以选择另一个共享工厂,其中包含 tablelist 的最新状态,并将该工厂注入您的两个控制器中。
  • @Mikey 当我什至将变量 $scope.selected 留在 cancel() 中时,列表中的更改仍然存在,尽管我已经定义了 dismiss('cancel')。

标签: javascript angularjs controller reset


【解决方案1】:

这可能是通过值|引用传递的案例。
阅读此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));
   }
}

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

【讨论】:

  • 感谢您的回答,但它不能正常工作。但我想我有一个解决办法。我只需要这个问题的答案。如何将选定对象发布回对象数组?例如我可以这样做reqService.post({data: selectedItem}) 吗?
  • 我可能误解了你的问题,有点不清楚,但我认为如果你想将一个对象发布到一个对象数组中,你必须这样做: reqService.post([{data : selectedItem}])
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-10
  • 1970-01-01
  • 1970-01-01
  • 2015-09-19
  • 2015-06-24
相关资源
最近更新 更多