javascript - 接收到服务信息后动态建表

标签 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>

Controller :

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

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

最佳答案

听起来您可以使用 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
})

关于javascript - 接收到服务信息后动态建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30921472/

相关文章:

javascript - AngularJS:深度对象修改后 View 未更新

javascript - 工作 $.ajax 调用的 AngularJS $http.post 版本返回 401 UNAUTHORIZED

javascript - 在选择更改时动态禁用输入

html - 为什么 <p> 嵌套在 <span> 中导致 block 元素?

javascript - 如何在单个 html 文件中使用 FontAwesome?

javascript - Angular.js 在 DOM 准备好之前显示加载动画

javascript - 在 magiczoomplus 中添加分享按钮

javascript - 如何在 React 中使网站主题在刷新时保持不变?

html - 为什么 <p> 标签中不能包含 <div> 标签?

javascript - 期望从 promise 分配的值(Jest unittest)