我正在编写一个从php文件返回狗品种列表的工厂,但我无法将响应数据返回给我的控制器。我有这样一个工厂在HTTP请求之后将数据从工厂返回给控制器AngularJS
angular.module('myApp')
.factory('DataService, function($http) {
return {
get_breeds: function() {
method: 'GET',
url: 'http://localhost:8080/php/api/getbreeds.php'
}).then(function onSuccess(response) {
console.log(response); // responds with 200
return response.data;
}).catch(function onError(err) {
console.log(err);
})
}
}
});
,这是我的组件
angular.module('myApp')
.component('homeComponent', {
templateUrl: 'home.component.html,
controller: function (DataService) {
DataService.get_breeds()
.then(function(response) {
var dog_breeds = response;
console.log(dog_breeds)
});
...
的一部分,但我没有收到任何返回和错误消息。有人能指引我朝着正确的方向吗?
不确定你是否复制并粘贴错误,但是这行'templateUrl:'home.component .html,'缺少一个结束单引号 – user2340824
啊啊,这是一个错误的C&P错误! –
您是否想要在'getBreeds'中返回'Promise'?该功能看起来不正确,语法荧光笔也困惑。或者你在'getBreeds'中使用注入'$ http'的地方? – sabithpocker