1

我想在我的应用程序中包含几个 3rd 方 Angular 模块。以前我只创建了简单的应用程序,我只是使用 ng-app 来引导并将我的代码放入控制器中。

据我了解,我的 html 中应该有这样的内容:

<html ng-app"myApp">

那么我的 JS 应该是这样的:

angular.module('myApp',['thirdPartyModule1','thirdPartyModule2']);

var myCtrl = function($scope, myApp, $http){

    //my stuff
};

但是当我这样做时,我得到一个错误:

Error: Unknown provider: myAppProvider <- myApp 
4

1 回答 1

9

You don't need to inject myApp into the controller. Your controller should be defined like this:

angular.module('myApp',['thirdPartyModule1','thirdPartyModule2']);

var myCtrl = function($scope, $http){
 //my stuff
});

to make it a little more "standard":

var myApp = angular.module('myApp',['thirdPartyModule1','thirdPartyModule2']);

myApp.controller('myCtrl', function($scope, $http){
  //my stuff
});

This way you can have a reference to your app if you like.

Now to make it compatible with minifiers/beautifiers/closure compiler:

var myApp = angular.module('myApp',['thirdPartyModule1','thirdPartyModule2']);

myApp.controller('myCtrl', ['$scope','$http', function($scope, $http){
  //my stuff
}]);

That basically makes the controller argument an array, where the first members of the array are what you're going to inject. This is because a minifier will turn the arguments into random letters like function(a,b) and angular won't know what the heck you want to inject. By passing strings in the array thy will be minified like so ['$scope','$http', function(a,b)] which is fine because the first two arguments in the array tell angular what to inject.

于 2013-08-07T03:43:52.853 回答