我对我的问题有点困惑。事实上,我有 2 个正在使用服务的视图和 ctrl。
第一个视图包含一个表格列表,其中包含将从 WebAPI 加载的项目。该服务向服务器发出请求并按订单提供。另外,我正在使用另一个服务来传输另一个 Ctrl 中的所选项目行。
这是代码:
View1:
//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 时,它仅重置模式窗口中的表单,但更改仍保留在表列表中?!我无法重置解析变量“选定”。