本文介绍了将ng-repeat绑定到诺言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将$ resource承诺绑定到ng-repeat.但我无法使其正常工作.
I'm trying to bind a $resource promise to ng-repeat. But I can't make it work.
HTML
<ul ng-controller="ColorsCtrl">
<li ng-repeat="colors in getColors()"></li>
</ul>
脚本
app.module("myApp", ['ngResource'])
.factory('Color', function($resource) {
return $resource('/colors.json');
})
.controller('ColorsCtrl', function($scope, Color) {
$scope.getColors = function() {
return Color.query();
}
});
推荐答案
您不需要将UI绑定到promise:将UI绑定到数组,然后填充数组:
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.
这篇关于将ng-repeat绑定到诺言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!