我从ipCity和ipCountry
获取未定义,未定义但我不明白为什么。
weatherApp.service('cityService', function ($resource) {
var ipLocation = $resource("http://ipinfo.io", {callback: "JSON_CALLBACK"}, {get: {method: "JSONP"}});
var ipResult = ipLocation.get();
var ipCity = ipResult.city;
var ipCountry = ipResult.country;
console.log(ipResult);
this.city = ipCity + ", " + ipCountry;
})
LOG:
d {$promise: d, $resolved: false}
$promise: d
$resolved: true
city: "Toronto"
country: "CA"
hostname: "CPE00fc8d503cf3-CM00fc8d503cf0.cpe.net.cable.rogers.com"
ip: "99.232.37.198"
loc: "43.6555,-79.3626"
org: "AS812 Rogers Cable Communications Inc."
postal: "M5A"
region: "Ontario"
__proto__: d
所以我基本上从API获取数据,但我无法将数据用于我的服务。
答案 0 :(得分:1)
您需要提供回调函数。另请注意,数据不会立即出现在您的服务中。
var self = this;
ipResult.$promise.then(function(data)
{
var ipCity = data.city;
var ipCountry = data.country;
console.log(data);
self.city = ipCity + ", " + ipCountry;
});