我有一个需要返回收藏位置列表的功能。像这样的东西
LocationsFactory.getFavoriteLocations()。then(function($ favoriteLocations){ });
getFavoriteLocations看起来像这样
getFavoriteLocations: function() {
if (favorite_locations.length == 0)
{
var deff = $q.defer();
obj.getDeviceId().then(function(device_id) {
$http.get('url?token=' + device_id).then(function(response) {
favorite_locations = response.data;
deff.resolve(favorite_locations);
return deff.promise;
})
})
} else {
return favorite_locations;
}
}
getDeviceId,它是一个基于promise的函数。
getDeviceId: function() {
var deff = $q.defer();
deff.resolve(Keychain.getKey());
return deff.promise;
}
我得到的错误是TypeError:无法读取未定义的属性'then'。请帮忙!
答案 0 :(得分:0)
$q
:
if (favorite_locations.length == 0)
{
return obj.getDeviceId() // you have to return a promise here
.then(function(device_id) {
return $http.get('url?token=' + device_id) // you will access the response below
})
.then(function(response) {
favorite_locations = response.data;
return favorite_locations
});
})
}
现在应该可以了。
答案 1 :(得分:0)
你可以链接承诺:
getFavoriteLocations: function () {
if (favorite_locations.length === 0) {
return obj.getDeviceId().then(function (device_id) {
return $http.get('url?token=' + device_id).then(function (response) {
favorite_locations = response.data;
return favorite_locations;
});
});
}
return $q.resolve(favorite_locations);
}
并改善这一点:
getDeviceId: function() {
return $q.resolve(Keychain.getKey());
}