Angularjs工厂:在传球$范围,$路径,$ HTTP错误? [英] Angularjs factory: errors on passing $scope,$route,$http?
问题描述
为什么下面我厂不断抛出一个错误,当 $范围,$路径,$ HTTP
是present?
app.factory(factoryContacts功能($范围,$路径,$ HTTP){ 返回{
getContacts:功能(){
返回$ HTTP({
方法:GET,
网址:PHP / listContacts.php
})。然后(功能(响应){
$ scope.contacts = response.data; });
}
};
});
我不能通 $范围,$路径,$ HTTP
进入工厂?
我这个基本一测试,它给了我同样的错误,
app.provider('的helloWorld',函数($范围内){//注$范围为present
//在提供的功能,你不能注入任何
//服务或工厂。这只能在完成
//$ get方法。 this.name =默认; 这一点。$ GET =功能(){
变量名称= this.name;
返回{
sayHello的:功能(){
返回你好,+名字+!从供应商!
}
}
}; this.setName =功能(名称){
this.name =名称;
};
});
我应该怎么办?
错误,
错误:[$喷油器:unpr] http://errors.angularjs.org/1.2.6/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope%20%3C-%20factoryContacts ...< NG-view类=NG-范围>
我不能通$范围,$路径,$ HTTP到工厂?
块引用>为什么不$范围
您不能注入$范围到服务/工厂,其单并拥有的本地范围的。但是你可以使用
$ rootScope
要通过
$路线
和$ HTTP
使用注射这样写:app.factory(factoryContacts,['$路线','$ HTTP',函数($路径,$ HTTP){ 返回{
getContacts:功能(){
返回$ HTTP({
方法:GET,
网址:PHP / listContacts.php
})。然后(功能(响应){
// .... });
}
};
}]);Why my factory below keep throwing an error when the
$scope, $route, $http
are present?app.factory("factoryContacts",function($scope,$route,$http){ return { getContacts: function(){ return $http({ method: 'GET', url:'php/listContacts.php' }).then(function(response) { $scope.contacts = response.data; }); } }; });
can't I pass
$scope, $route, $http
into a factory?I tested with this basic one and it gives me the same error,
app.provider('helloWorld', function($scope) { // note $scope is present // In the provider function, you cannot inject any // service or factory. This can only be done at the // "$get" method. this.name = 'Default'; this.$get = function() { var name = this.name; return { sayHello: function() { return "Hello, " + name + "! From Provider!!" } } }; this.setName = function(name) { this.name = name; }; });
What should I do?
the error,
Error: [$injector:unpr] http://errors.angularjs.org/1.2.6/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope%20%3C-%20factoryContacts ... <ng-view class="ng-scope">
解决方案can't I pass $scope, $route, $http into a factory?
why not $scope
You can't inject $scope into service/factory, its singleton and has local scope. But you can use
$rootScope
To pass
$route
and$http
use injection and write like:app.factory("factoryContacts",['$route','$http', function($route,$http){ return { getContacts: function(){ return $http({ method: 'GET', url:'php/listContacts.php' }).then(function(response) { //.... }); } }; }]);
这篇关于Angularjs工厂:在传球$范围,$路径,$ HTTP错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!