我是 Angular JS 新手,正在尝试使用 JSON 数据调用 Rest API。当我运行 HTTP-server 时,我没有取回我的响应数据。
function contacts(contactsdata) {
contactsdata.getcontacts().then(function(data) {
this.contactsinfo = data;
});
}
(function(){
var mod = angular.module('myapp');
mod.controller("contacts", contacts);
mod.service("contactsdata", function($http) {
this.getcontacts = function(){
var execute1 = $http.get('http://localhost:3000/contacts');
var execute2 = execute1.then(function(response) {
return response.data;
})
return execute2;
}
});
})();
最佳答案
您可以在服务中尝试此操作
this.getcontacts = function(){
return $http.get('http://localhost:3000/contacts');
});
并用作
contactsdata.getcontacts().then(function(response){
this.contactsinfo = response.data;
});
关于javascript - Angular JS 数据响应未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46169165/