Javascript 重置更改后的值不会';t在其他Ctrl(AngularJS)模式下工作
我对我的问题有点困惑。事实上,我有两个视图和ctrl正在使用服务。 第一个视图包含一个表列表,其中包含将从WebAPI加载的项。该服务向服务器发出请求并提供订单。另外,我正在使用另一个服务在另一个Ctrl中传输所选项目行。 代码如下: 视图1:Javascript 重置更改后的值不会';t在其他Ctrl(AngularJS)模式下工作,javascript,angularjs,controller,reset,Javascript,Angularjs,Controller,Reset,我对我的问题有点困惑。事实上,我有两个视图和ctrl正在使用服务。 第一个视图包含一个表列表,其中包含将从WebAPI加载的项。该服务向服务器发出请求并提供订单。另外,我正在使用另一个服务在另一个Ctrl中传输所选项目行。 代码如下: 视图1: //view1.html <table class="table table-bordered table-hover"> <thead> <tr> <th>Firstname<
//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>
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”。它可能是一个case pass by value|reference。
读这个 如果您尝试传递一个重置值的函数,那么看看它是如何工作的,这会很有趣 一种可能的实现方式 Ctrl1:
Ctrl2开始:
好的,我知道它很脏,但这只是为了测试假设。有趣的问题,你可以去另一个共享工厂,它包含tablelist的最新状态,并将该状态注入到你的两个控制器中。@Mikey当我甚至离开变量
$scope时。在取消()
中选择,列表中的更改仍然保留,虽然我已经为您的答案定义了discouse('cancel')
.Thx,但它不能正常工作。但我想我有一个解决办法。我只需要回答这个问题。如何将选定对象发布回对象数组?例如,我可以这样做reqService.post({data:selectedItem})
?我可能误解了你的问题,它有点不清楚,但我认为如果你想将一个对象发布到一个对象数组中,你必须这样做:reqService.post([{data:selectedItem}])
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
}
}
$scope.open = function (item) {
$scope.selectedItem = item;
modalService.openDialog($scope.namelist, function(e){
if (e === undefined) {
return $scope.selectedItem;
} else {
$scope.selectedItem = e;
}
});
}
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));
}
}