问题
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