1

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
sof
  • 9,113
  • 16
  • 57
  • 83
  • ng-view is a directive which invokes routeprovider config ,why do you want to create your own directive? – Ajay Beniwal Apr 12 '13 at 09:29
  • What i wish to achieve is to remove the boilerplate invocation of `angular#module#config`. I'm unsure if it's feasible. – sof Apr 12 '13 at 09:34
  • @sof You can define the routes within the module call: angular.module('myApp', [], ['$routeProvider', function($routeProvider){...}]. I don't know why implementing an extra directive is needed. – F Lekschas Apr 12 '13 at 09:39
  • My initial impression on `angularjs` is that the `route provider` could be made similarly to `ng-controller` with `code-behind` controllers but rather away from `injected services` with the aid of calling `angular#module`. Could be wrong or worthless. – sof Apr 12 '13 at 09:58

0 Answers0