在从服务接收信息后动态构建表

时间:2015-06-18 17:01:10

标签: javascript html angularjs asynchronous angularjs-directive

HTML代码:

<table class="table table-bordered table-striped">
    <thead>
        <tr>
            <td>one</td>
            <td>two</td>
            <td>three</td>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="color in colors">
            <td>{{ color.one }}</td>
            <td>{{ color.two }}</td>
            <td>{{ color.three }}</td>
        </tr>
    </tbody>
</table>

控制器:

var SomeController = function ($scope, SomeService, $window) {
    var colors= [];

    SomeService.someFunction().success(function (data, status, header, config) {
        colors= data;
    }).error(function (data, status, headers, config) {
        // handle error
    });
};

基本上,我需要根据后端的内容动态创建我的表格...但是,问题是,一旦我调用页面,它会自动加载列,一秒钟左右,它会自动加载列。我会从服务中恢复过来。无论如何,我可以延迟加载时间或让表格刷新&#34;本身就是为了显示新数据。

1 个答案:

答案 0 :(得分:1)

听起来您可以使用ngShow并等待您的数据在$scope.colors上解决。这应该会延迟显示您的表格直到通话完成。请尝试以下方法......

<table 
    class="table table-bordered table-striped"
    ng-show="colors" class="ng-hide">
    <thead>
        <tr>
            <td>one</td>
            <td>two</td>
            <td>three</td>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="color in colors">
            <td>{{ color.one }}</td>
            <td>{{ color.two }}</td>
            <td>{{ color.three }}</td>
        </tr>
    </tbody>
</table>
SomeService.someFunction().success(function (data, status, header, config) {
    $scope.colors = data; // let colors be defined on $scope
})