为什么我的Restangular对象是空的?
问题描述:
我正在创建一个小应用程序。我需要github表情符号来加载它。我正在使用restangular。 我设置restangular的baseUrl为:为什么我的Restangular对象是空的?
RestangularProvider.setBaseUrl('https://api.github.com');
我需要的是现在我想这是我一个数组中定义的表情符号的一些公司。那就是:
$scope.emojiNames = ['tokyo_tower', 'tongue', 'tractor', 'traffic_light', 'train', 'tiger', 'red_car'];
$scope.emojisObj = Restangular.one("emojis").get().
then(function(res) {
return ($scope.emojiNames).map(function(emoji) {
return res[emoji];
}, function(err) {
console.log("ERR:", err);
});
});
虽然安慰emojisObj
,它显示相应的URL值。
但在Html中绑定emojisObj
它显示为空。我在哪里犯错误?
我的HTML代码是:
<span ng-repeat="emoji in emojisObj track by $index">
<img ng-src="{{emoji}}" height="100px" width="100px"/>
</span>
答
你不能从里面承诺返回。你在这里做的是,你正在返回Restangular.one()得到$范围,如果要取消映射它应该像THI
Restangular.one("emojis").get().then(function(res) {
var mapped = [];
$scope.emojiNames).map(function(emoji) {
mapped.push(res[emoji]);
});
$scope.emojisObj = mapped;
}, function(err) {
console.log("ERR:", err);
});
或者直接
Restangular.one("emojis").get().then(function(res) {
$scope.emojisObj = [];
$scope.emojiNames).map(function(emoji) {
$scope.emojisObj.push(res[emoji]);
});
}, function(err) {
console.log("ERR:", err);
});
进行响应