Angularjs 如何在缩小JS时包含$http

Angularjs 如何在缩小JS时包含$http,angularjs,Angularjs,由于我正在缩小和丑化我的JS,我不能简单地做以下事情: var app = angular.module('app', []); app.controller('MyController', function($scope, $http) { // ... }); 它将抛出一个与注入相关的错误,因为缩小它会混淆$scope变量 相反,我必须这样做: app.controller('MyController', ['$scope', function($scope) { $scope.ang

由于我正在缩小和丑化我的JS,我不能简单地做以下事情:

var app = angular.module('app', []);
app.controller('MyController', function($scope, $http) {
// ...
});
它将抛出一个与注入相关的错误,因为缩小它会混淆$scope变量

相反,我必须这样做:

app.controller('MyController', ['$scope', function($scope) {
  $scope.angularIs = "awesome";
}]);
问题:使用第二种方法,如何添加http服务?像这样的

app.controller('MyController', ['$scope&$http', function($scope, $http) {
// ...
}]);
我的
angular.module('app',[])也可以声明保持不变,还是必须向
[]
添加某种类型的http依赖项?谢谢

使用以下命令:

app.controller('MyController', ['$scope','$http', function($scope, $http) {
// ...
}]);
您只需使用:

app.controller('MyController', ['$scope', '$http', function($scope, $http) {
  // ...
}]);

然后它将与函数()中的参数匹配。你的应用程序的依赖项总是字符串,所以缩小后就可以了。

顺便说一句,使用gulp/grunt之类的构建工具,你可以自动添加这些,这样你就不必到处“双重注入”。