Bind ng-repeat to a promise

女生的网名这么多〃 提交于 2019-12-23 20:16:16

问题


I'm trying to bind a $resource promise to ng-repeat. But I can't make it work.

The HTML

<ul ng-controller="ColorsCtrl">
   <li ng-repeat="colors in getColors()"></li>
</ul>

The Script

app.module("myApp", ['ngResource'])

.factory('Color', function($resource) {
    return $resource('/colors.json');
})

.controller('ColorsCtrl', function($scope, Color) {
    $scope.getColors = function() {
       return Color.query();
    }
});

回答1:


You don't need to bind your UI to a promise: you bind the UI to an array, then you fill the array:

$scope.colors = [];

var colors = Colors.query(function() {
   // fill here $scope.colors
});

While asynchronous request is being processed, usually you show a loading orb in the place where the data is going to be displayed.



来源:https://stackoverflow.com/questions/27858592/bind-ng-repeat-to-a-promise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!