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

时间:2014-11-02 13:29:44

标签: angularjs

由于我正在缩小和丑化我的JS,我不能简单地执行以下操作:

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

它会抛出一个与注入相关的错误,因为它会缩小$ scope var。

相反,我必须这样做:

app.controller('MyController', ['$scope', function($scope) {
  $scope.angularIs = "awesome";
}]);

问题:使用第二种方法,如何添加http服务?像这样的东西?

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

我的angular.module('app', []);声明是否可以保持不变,或者我是否必须向[]添加某种类型的http依赖关系?谢谢!

2 个答案:

答案 0 :(得分:0)

使用它:

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

答案 1 :(得分:0)

你可以使用:

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

然后它将匹配function()中的参数。您的应用程序的依赖项将始终是字符串,因此在缩小时这些将是正常的。