之前有人問過這個(gè)問題,從答案來看它看起來并不好。我想考慮一下此示例代碼...我的應(yīng)用程序?qū)?dāng)前項(xiàng)目加載到提供它的服務(wù)中。有幾個(gè)控制器可以在不重新加載項(xiàng)目的情況下操縱項(xiàng)目數(shù)據(jù)。如果尚未設(shè)置,我的控制器將重新加載該項(xiàng)目,否則,它將在控制器之間使用該服務(wù)中當(dāng)前加載的項(xiàng)目。問題:我想為每個(gè)控制器使用不同的路徑,而無需重新加載Item.html。1)有可能嗎?2)如果這不可能,那么與我在這里提出的相比,是否有更好的方法為每個(gè)控制器設(shè)置路徑?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);}解析度。狀態(tài):按照接受的答案中的建議使用搜索查詢,這使我可以提供不同的URL,而無需重新加載主控制器。我的局部中的內(nèi)容用 ng-controller=...
您可以在不重新加載AngularJS中的控制器的情況下更改路徑嗎?
揚(yáng)帆大魚
2019-10-15 15:06:55