Angularjs 何处注入;“事物”;
我有一个app.js文件,看起来像这样:Angularjs 何处注入;“事物”;,angularjs,Angularjs,我有一个app.js文件,看起来像这样: var app = angular.module('myApp', [$http]); app.controller('httpController', function($http) { $scope.httpCommand = function ($http) { //http stuff in here }; }); }); 我不知道在哪里注入依赖项。例如,我需要$http。在应用程序、控
var app = angular.module('myApp', [$http]);
app.controller('httpController', function($http) {
$scope.httpCommand = function ($http) {
//http stuff in here
};
});
});
我不知道在哪里注入依赖项。例如,我需要$http。在应用程序、控制器或函数本身中,我将在何处注入此项?控制器构造函数中已经声明了$http依赖项。然后,angular injector服务将向您传递$http实例。然而,声明依赖项的首选方法是使用内联数组注释,这将防止在缩小/压缩javascript文件时发现冲突。例如:
var app = angular.module('myApp', [$http]);
app.controller('httpController',['$http', function($http) {
$scope.httpCommand = function () {
//http stuff in here,
$http.get("www.someurl.com", function(result) {
//do something with result
});
};
}]);
});
由于关闭,控制器作用域中已有一个
$http
。有关更多信息,请参阅AngularJS文档中的。我是否也需要将其包含在模块依赖项中?AngularJS中是否有充分的理由传递参数,即使参数已经在范围内?它似乎是多余的,但可能有一个很好的理由。@user2924127-不,在模块注册中不需要它。这是angular的一部分。完美!谢谢你的帮助@达文尼顿-不,不是在这种情况下。