Angularjs在不同文件中路由

Har*_*rry 34 routing angularjs angular-routing

我正在检查角度路由.

http://www.bennadel.com/blog/2420-Mapping-AngularJS-Routes-Onto-URL-Parameters-And-Client-Side-Events.htm

我看到的例子中所有路由都在同一个文件中定义.如何在不同的文件/模块中定义各种路由?

pko*_*rce 57

在AngularJS中,路由在配置块中定义.每个AngularJS模块都可以有多个配置块,您可以在每个配置块中定义路由.整个应用程序的最终路由是所有模块中定义的路由总和.

在实践中你可以这样做:

angular.module('myModule1', []).config(function($routeProvider){
  //define module-specific routes here
});

angular.module('myModule2', []).config(function($routeProvider){
  //define module-specific routes here
});

angular.module('myApp', ['myModule1', 'myModule2']).config(function($routeProvider){
  //define app-level routes here
});
Run Code Online (Sandbox Code Playgroud)

关于文件拆分 - 我想这在很大程度上取决于你如何在文件中拆分AngularJS模块.我建议坚持单文件等于单模块原则.

您可以在angular-app中看到所有这些应用于更大规模的Web应用程序,努力为AngularJS编写的非平凡应用程序构建引用:

在上述应用程序中,您可以看到在多个文件中定义的路由,例如: