Angular directive for route provider

2019-07-29 14:15发布

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

0条回答
登录 后发表回答