之前有人問過這個問題,從答案來看它看起來并不好。我想考慮一下此示例代碼...我的應用程序將當前項目加載到提供它的服務中。有幾個控制器可以在不重新加載項目的情況下操縱項目數據。如果尚未設置,我的控制器將重新加載該項目,否則,它將在控制器之間使用該服務中當前加載的項目。問題:我想為每個控制器使用不同的路徑,而無需重新加載Item.html。1)有可能嗎?2)如果這不可能,那么與我在這里提出的相比,是否有更好的方法為每個控制器設置路徑?app.jsvar app = angular.module('myModule', []). config(['$routeProvider', function($routeProvider) { $routeProvider. when('/items', {templateUrl: 'partials/items.html', controller: ItemsCtrl}). when('/items/:itemId/foo', {templateUrl: 'partials/item.html', controller: ItemFooCtrl}). when('/items/:itemId/bar', {templateUrl: 'partials/item.html', controller: ItemBarCtrl}). otherwise({redirectTo: '/items'}); }]);Item.html<!-- Menu --><a id="fooTab" my-active-directive="view.name" href="#/item/{{item.id}}/foo">Foo</a><a id="barTab" my-active-directive="view.name" href="#/item/{{item.id}}/bar">Bar</a><!-- Content --><div class="content" ng-include="" src="view.template"></div>controller.js// Helper function to load $scope.item if refresh or directly linkedfunction itemCtrlInit($scope, $routeParams, MyService) { $scope.item = MyService.currentItem; if (!$scope.item) { MyService.currentItem = MyService.get({itemId: $routeParams.itemId}); $scope.item = MyService.currentItem; }}function itemFooCtrl($scope, $routeParams, MyService) { $scope.view = {name: 'foo', template: 'partials/itemFoo.html'}; itemCtrlInit($scope, $routeParams, MyService);}function itemBarCtrl($scope, $routeParams, MyService) { $scope.view = {name: 'bar', template: 'partials/itemBar.html'}; itemCtrlInit($scope, $routeParams, MyService);}解析度。狀態:按照接受的答案中的建議使用搜索查詢,這使我可以提供不同的URL,而無需重新加載主控制器。我的局部中的內容用 ng-controller=...
3 回答

慕神8447489
TA貢獻1780條經驗 獲得超1個贊
為什么不只是將ng-controller提升一級,
<body ng-controller="ProjectController">
<div ng-view><div>
而且不要在路線中設置控制器,
.when('/', { templateUrl: "abc.html" })
這個對我有用。
- 3 回答
- 0 關注
- 651 瀏覽
添加回答
舉報
0/150
提交
取消