让我们说我是一个非常大的企业应用程序,细分为几个解耦的子模块(不要与角度模块混淆)。
我不想在一个地方膨胀路由,并希望这些独立模块(非角度模块)拥有自己的角度和路由模块,否则我就要向每个子模块广播路由通知。如果这些子模块可以拥有自己的路由侦听器或定义,则非常理想。
Angular新手。
模块A
angular.module("navigation", ["ngRoute"]).
config(function($routeProvider, $locationProvider){self.config($routeProvider, $locationProvider)}).
controller("FoliosController", function($scope, $location){self.foliosController($scope, $location)}).
controller("NavigationController", function($scope, $location, $routeParams){self.navigationController($scope, $location, $routeParams)});
angular.bootstrap(document.getElementById("navigation"), ["navigation"]);
//config part
config: function($routeProvider, $locationProvider) {
$routeProvider.
when("/", {templateUrl:"js/modules/search/view/partials/navigation.html"}).
when("/search/folios/:productId", {controller:"NavigationController", templateUrl:"js/modules/search/view/partials/navigation.html"})
},
模块B
var result = angular.module("result", ["ngRoute"]).
config(function($routeProvider, $locationProvider){self.config($routeProvider, $locationProvider)}).
controller("FoliosResultsController", function($scope, $location) {self.foliosResultsController($scope, $location)})
angular.bootstrap(document.getElementById("result"), ["result"]);
//config part
config: function($routeProvider, $locationProvider) {
$routeProvider.
when("/", {templateUrl:"js/modules/search/view/partials/result.html"}).
when("/search/folios/:productId", {controller:"NavigationController", templateUrl:"js/modules/search/view/partials/result.html"})
},
模块B路由不起作用,所以我不确定我们是否可以在多个地方拥有路由侦听器。
HTML
<div id="navigation" data-ng-cloak>
<ul id="folios" data-ng-controller="FoliosController" class="nav nav-pills nav-stacked">
<li data-ng-repeat="folio in folios" ng-class="{active: isActive('/search/folios/{{folio.productId}}')}">
<a href="#/search/folios/{{folio.productId}}">{{folio.title}}</a>
</li>
</ul>
<ng-view></ng-view>
</div>
编辑: 当我第一次加载文档时,两个路由器都针对/ search / folios /:productId执行 但是一旦点击列表项,只有模块A的路由器执行。
答案 0 :(得分:0)
破解了。
ui-router允许多个状态&amp; url路由器提供程序,用于单页面应用程序中的不同模块(角度模块)。基本上我的每个模块(软件模块)都拥有它自己的角度模块&#34;使用它自己的路由引擎/状态机。
我的每个模块(软件模块及其角度模块)都接收并可以根据状态定义响应散列更改。即使它们已被定义,例如,如果&#34; route1&#34;已经为另一个模块定义,它也会响应。
原因:模块化和解耦系统。很酷,这个项目现在更有条理。
修改强> 在多个地方进行路由,将其置于中心位置并将广播路由更改为所有模块是一种不好的方法。