【发布时间】:2026-01-29 15:40:01
【问题描述】:
我提供了一项服务,我正在获取数据,但它没有向控制器返回任何内容。
我的服务。 js文件
app.factory('countryService', function ($http) {
return {
getCountries: function () {
$http.get('http://abc/countries')
.success(function (data) {
console.log(data);
})
.error(function (data) {
console.log(data);
});
}
};
});
这是我的控制器
$scope.countries = function () {
$scope.countries_data = countryService.getCountries();
console.log($scope.countries_data);
};
我的错误是什么?
【问题讨论】:
-
尝试使用
return语句。有关详细信息,请参阅MDN JavaScript Reference -- return statement。 -
@georgeawg only
return不起作用,因为.success&.error函数已应用于$http.get,它将破坏承诺链,OP 必须删除.success& @ 987654330@函数(这里也没什么特别的)
标签: angularjs http service factory