重置更改后的值在其他 Ctrl (AngularJS) 中不起作用 [英] Reset the changed values doesn't work in other Ctrl (AngularJS)
问题描述
我对我的问题有点困惑.事实上,我有 2 个视图和 ctrl 正在使用服务.第一个视图包含一个表列表,其中包含将从 WebAPI 加载的项目.该服务向服务器发出请求并提供订单.此外,我正在使用另一个服务来传输另一个 Ctrl 中的选定项目行.这是代码:
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 服务:
//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 时,它只会重置我的模式窗口中的表单,但更改仍保留在表格列表中?!我无法重置解析变量selected".
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".
推荐答案
可能是 case pass by value|reference.
阅读这篇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:
$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屋!