【问题标题】:Why my Restangular object is empty?为什么我的 Restangular 对象是空的?
【发布时间】:2015-09-28 11:04:12
【问题描述】:

我正在创建一个小应用程序。我需要将 github 表情符号加载到其中。我正在使用restangular。 我已将 restangular baseUrl 设置为:

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>

【问题讨论】:

    标签: angularjs restangular


    【解决方案1】:

    你不能从内部承诺返回。你在这里做什么是你正在返回 Restangular.one().get 到 $scope 如果你想取消映射它应该像这样完成的响应

    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);
    });
    

    【讨论】:

      猜你喜欢
      • 2015-09-04
      • 2017-09-10
      • 2014-06-21
      • 2016-12-02
      • 1970-01-01
      • 2017-04-27
      • 2016-02-13
      • 1970-01-01
      相关资源
      最近更新 更多