Javascript 如何为AngularJS服务分配属性/方法
我创建了一个角度服务:Javascript 如何为AngularJS服务分配属性/方法,javascript,angularjs,controller,Javascript,Angularjs,Controller,我创建了一个角度服务: app.service('myService', ['$http', '$q', function($http, $q){ var getInfo = function(){ return $http({ method: 'GET', url: 'X' }); }; }]); app.controller('myController', ['$scope', 'myServ
app.service('myService', ['$http', '$q', function($http, $q){
var getInfo = function(){
return $http({
method: 'GET',
url: 'X'
});
};
}]);
app.controller('myController', ['$scope', 'myService', function ($scope, myService) {
myService.getInfo()
.success(function(data, status, headers) {
})
.error(function(data, status, headers) {
})
}]);
TypeError: Object [object Object] has no method 'getInfo'
app.factory('myService2', function() {
var myService2 = {
foo: function() {
console.log('foo from myService2');
}
};
//what you return from a factory is the service
return myService2;
});
而控制器无法识别它:
app.service('myService', ['$http', '$q', function($http, $q){
var getInfo = function(){
return $http({
method: 'GET',
url: 'X'
});
};
}]);
app.controller('myController', ['$scope', 'myService', function ($scope, myService) {
myService.getInfo()
.success(function(data, status, headers) {
})
.error(function(data, status, headers) {
})
}]);
TypeError: Object [object Object] has no method 'getInfo'
app.factory('myService2', function() {
var myService2 = {
foo: function() {
console.log('foo from myService2');
}
};
//what you return from a factory is the service
return myService2;
});
我在控制台中收到此错误:
app.service('myService', ['$http', '$q', function($http, $q){
var getInfo = function(){
return $http({
method: 'GET',
url: 'X'
});
};
}]);
app.controller('myController', ['$scope', 'myService', function ($scope, myService) {
myService.getInfo()
.success(function(data, status, headers) {
})
.error(function(data, status, headers) {
})
}]);
TypeError: Object [object Object] has no method 'getInfo'
app.factory('myService2', function() {
var myService2 = {
foo: function() {
console.log('foo from myService2');
}
};
//what you return from a factory is the service
return myService2;
});
我缺少什么?t这应该是
这个.getInfo
,以便getInfo
被指定为服务的一种方法
app.service('myService', ['$http', '$q', function($http, $q){
this.getInfo = function(){
return $http({
method: 'GET',
url: 'X'
});
};
}]);
以下是使用Angular创建服务的两个通用示例:
app.service('myService', function() {
// "this" is the service
this.foo = function() {
console.log('foo from myService1');
};
});
使用工厂:
app.service('myService', ['$http', '$q', function($http, $q){
var getInfo = function(){
return $http({
method: 'GET',
url: 'X'
});
};
}]);
app.controller('myController', ['$scope', 'myService', function ($scope, myService) {
myService.getInfo()
.success(function(data, status, headers) {
})
.error(function(data, status, headers) {
})
}]);
TypeError: Object [object Object] has no method 'getInfo'
app.factory('myService2', function() {
var myService2 = {
foo: function() {
console.log('foo from myService2');
}
};
//what you return from a factory is the service
return myService2;
});