Angular directive for route provider

吃可爱长大的小学妹 提交于 2019-12-24 10:45:34

问题


The angular tutorial invokes the route provider explicitly below, (being adapted for the sake of succinctness)

angular.module('myApp', []).
  config(['$routeProvider', function($routeProvider) {
    $routeProvider.
      when('/myResource1', 
        {templateUrl: 'partials/myView1.html', controller: MyController1}).
      when('/myResource2', 
        {templateUrl: 'partials/myView2.html', controller: MyController2}).
      otherwise({redirectTo: '/index.html'});
  }]);

Can we define the route provider separately, then invoke it by means of angular directive below? Is there such a directive existing? Otherwise, how to make through with it?

function MyRouteProvider($routeProvider) {
    $routeProvider.
      when('/myResource1', 
        {templateUrl: 'partials/myView1.html', controller: MyController1}).
      when('/myResource2', 
        {templateUrl: 'partials/myView2.html', controller: MyController2}).
      otherwise({redirectTo: '/index.html'});
};
<html ng-app="myApp">
<head>
  <script src="lib/angular.js"></script>
  <script src="js/MyControllers.js"></script>
  <script src="js/MyRouteProvider.js"></script>
</head>
<body ng-routeProvider="MyRouteProvider">

...

</body>
</html>
myApp
  |__index.html
  |__js
  |   |__MyControllers.js
  |   |__MyRouteProvider.js
  |__partials
      |__myView1.html
      |__myView2.html

来源:https://stackoverflow.com/questions/15967613/angular-directive-for-route-provider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!