【问题标题】:Dynamically build table after it recieves information from service收到服务信息后动态建表
【发布时间】:2015-06-19 01:14:12
【问题描述】:

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
    });
};

基本上,我需要根据后端中的任何内容动态创建我的表......但是,问题是一旦我调用页面,它会自动加载列,然后一秒钟左右,它会得到从服务中返回。无论如何我可以延迟加载时间或让表格“刷新”自身以显示新数据。

【问题讨论】:

  • subscriptions 包含在$scope.subscriptions= data 这样的范围内,然后ng-repeat 将是tr ng-repeat="subscription in subscriptions"&gt; &lt;td&gt;{{ subscription.one}}&lt;/td&gt; &lt;td&gt;{{ subscription.two }}&lt;/td&gt; &lt;td&gt;{{ subscription.three }}&lt;/td&gt; &lt;/tr&gt;

标签: javascript html angularjs asynchronous angularjs-directive


【解决方案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
})

【讨论】:

  • 成功了!我会确保尽可能将其标记为正确答案。谢谢!
猜你喜欢
  • 1970-01-01
  • 2012-05-07
  • 1970-01-01
  • 1970-01-01
  • 2017-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多