使用AngularJS的多个配置提供程序

使用AngularJS的多个配置提供程序,angularjs,Angularjs,如何为AngularJS指定多个提供者 现在我在两个地方做,但这似乎不起作用: var myApp = angular.module("myApp", ['ngRoute']); // configure our routes myApp.config(function($routeProvider) { $routeProvider // route for the home page .when('/', { templateUrl : 'pages/home.html'

如何为AngularJS指定多个提供者

现在我在两个地方做,但这似乎不起作用:

var myApp = angular.module("myApp", ['ngRoute']);

// configure our routes
myApp.config(function($routeProvider) {
$routeProvider

  // route for the home page
  .when('/', {
    templateUrl : 'pages/home.html',
    controller  : 'mainController'
  })

  // route for the about page
  .when('/about', {
    templateUrl : 'pages/about.html',
    controller  : 'aboutController'
  })

  // route for the contact page
  .when('/contact', {
    templateUrl : 'pages/contact.html',
    controller  : 'contactController'
  });
}))


使用单个函数引用:

myApp.config(foo)
它注入每个提供者:

function foo($routeProvider, $interpolateProvider) 
 {     
 /* interpolateProvider configuration */
 $interpolateProvider.startSymbol('((').endSymbol('))');

 /* routeProvider configuration */
 $routeProvider
  .when('/', {
    templateUrl : 'pages/home.html',
    controller  : 'mainController'
  })
  .when('/about', {
    templateUrl : 'pages/about.html',
    controller  : 'aboutController'
  })
  .when('/contact', {
    templateUrl : 'pages/contact.html',
    controller  : 'contactController'
  });
 }

/* module creation */
var myApp = angular.module("myApp",[]);

/* provider binding */
myApp.config(foo);

/* Manual bootstrapping */
angular.bootstrap(document, ['myApp']);

 /* $inject Property Annotation */
 foo.$inject['$routeProvider', '$interpolateProvider'];
提供者本质上是用于创建和配置AngularJS人工制品实例的对象。因此,为了注册惰性控制器,您将使用$controllerProvider。类似地,要注册指令,可以使用$compileProvider;要注册过滤器,可以使用$filterProvider;要注册其他服务,可以使用$provide服务

参考资料


要准确。“这似乎不起作用”无助于我们确定问题所在。到底发生了什么?你有什么错误吗?您预期会发生什么以及会发生什么?注入应该是
foo.$inject=['$routeProvider','$interpolateProvider']
function foo($routeProvider, $interpolateProvider) 
 {     
 /* interpolateProvider configuration */
 $interpolateProvider.startSymbol('((').endSymbol('))');

 /* routeProvider configuration */
 $routeProvider
  .when('/', {
    templateUrl : 'pages/home.html',
    controller  : 'mainController'
  })
  .when('/about', {
    templateUrl : 'pages/about.html',
    controller  : 'aboutController'
  })
  .when('/contact', {
    templateUrl : 'pages/contact.html',
    controller  : 'contactController'
  });
 }

/* module creation */
var myApp = angular.module("myApp",[]);

/* provider binding */
myApp.config(foo);

/* Manual bootstrapping */
angular.bootstrap(document, ['myApp']);

 /* $inject Property Annotation */
 foo.$inject['$routeProvider', '$interpolateProvider'];