【发布时间】: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