我正在阅读 http://www.alexrothenberg.com/2013/02/11/the-magic-behind-angularjs-dependency-injection.html,结果发现如果你缩小你的javascript,angularjs依赖注入有问题,所以我我想知道是否代替
var MyController = function($scope, $http) {
$http.get('https://api.github.com/repos/angular/angular.js/commits')
.then(function(response) {
$scope.commits = response.data
})
}
Run Code Online (Sandbox Code Playgroud)
你应该使用
var MyController = ['$scope', '$http', function($scope, $http) {
$http.get('https://api.github.com/repos/angular/angular.js/commits')
.then(function(response) {
$scope.commits = response.data
})
}]
Run Code Online (Sandbox Code Playgroud)
总而言之,我认为第二个片段是旧版本的angularjs,但....
我应该总是使用注入方式(第二个)吗?
缩小和AngularJS有一些麻烦;-(
我通过AngularJS Wiki页面找到了这个jsfiddle "加载"扩展器的HTTP请求.
它发布之前一直很有效,并且缩小了它.我找不到在配置上使用"注入"的方法,所以我有点失去了该怎么做.
原始代码:
angular.module("app.services", []).config(function($httpProvider) {
var spinnerFunction;
$httpProvider.responseInterceptors.push("myHttpInterceptor");
spinnerFunction = function(data, headersGetter) {
$("#loader").show();
return data;
};
return $httpProvider.defaults.transformRequest.push(spinnerFunction);
}).factory("myHttpInterceptor", function($q, $window) {
return function(promise) {
return promise.then((function(response) {
$("#loader").hide();
return response;
}), function(response) {
$("#loader").hide();
return $q.reject(response);
});
};
});
Run Code Online (Sandbox Code Playgroud)
缩小代码:
angular.module("app.services", []).config(function (a) {
var b;
a.responseInterceptors.push("myHttpInterceptor");
b = function (d, c) {
$("#loader").show();
return d
};
return a.defaults.transformRequest.push(b)
}).factory("myHttpInterceptor", function (a, b) {
return function (c) {
return c.then((function (d) {
$("#loader").hide(); …Run Code Online (Sandbox Code Playgroud)