复位更改的值在其他Ctrl键不起作用(AngularJS) [英] Reset the changed values doesn't work in other Ctrl (AngularJS)

查看:128
本文介绍了复位更改的值在其他Ctrl键不起作用(AngularJS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对我的问题有点糊涂了。其实我已经2次和Ctrl谁与服务工作。
首先查看包含将从一个的WebAPI加载项的tablelist。该服务使请求到服务器,并提供订购。另外我在使用其他服务所选择的项目行中的其他Ctrl键转移。
这里的code:

I'm a bit confused about my problem. In fact I've 2 views and ctrl who are working with a service. First View contains a tablelist with the items which will load from a WebAPI. The service makes requests to the server and provides to order. Also I'm using another service to transfer the selected item row in the other Ctrl. Here's the Code:

视图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的服务:

HTTP-Service:

//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
   }
}

我的问题是我怎么能重置tablelist?当我点击resetBtn,它在我的模式窗口重置只是形式,但变化保留在表列表?我不能复位选择的决心变量。

My question is how can I reset the tablelist? When I click on resetBtn, it resets only the form in my modal window but the changes remain in the table list?! I cannot reset the resolve variable "selected".

推荐答案

这可能是由值的情况下通|。参考结果
阅读本<一个href=\"http://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language\">Is JavaScript的一个传递按引用传递,或按值语言?。

It could be a case pass by value|reference.
Read this Is JavaScript a pass-by-reference or pass-by-value language? .

这将看看它是如何工作的,如果你试图传递的重置价值,而不是一个功能很有趣。

It would be interesting to see how it works if you try to pass a function that reset the value instead.

CTRL1:结果

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开始:结果

Ctrl2 began :

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));
   }
}

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

Ok I know it's dirty but it's just to test the assumption.

这篇关于复位更改的值在其他Ctrl键不起作用(AngularJS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆