本文介绍了将 ng-repeat 绑定到一个 promise的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 $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 绑定到一个 promise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 03:34