首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >转角转台折光

转角转台折光
EN

Stack Overflow用户
提问于 2016-09-06 16:14:10
回答 2查看 2.4K关注 0票数 3

我有一个在页面加载时调用的api。api中的数据通过角纳克重复加载到表中。我还有一个javascript函数,它每10秒调用一次,为相同的数据集调用相同的api。我想知道如何将新的数据集应用到表中,并在数据集发生变化时替换旧的数据集,以及如何用动画直观地显示此更改。代码在下面。

表码

代码语言:javascript
运行
复制
<body ng-app="myApp" ng-controller="ScansController">
<div class="bs-example" id="scan-table">
    <table id="scansTable" class="table table-striped">
        <thead>
            <tr>
                <th>ScanId</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Time Stamp</th>
            </tr>
            <tr ng-repeat="scan in Scans">
                <td>
                    {{scan.scanId}}
                </td>
                <td>
                    {{scan.firstName}}
                </td>
                <td>
                    {{scan.lastName}}
                </td>
                <td>
                    {{scan.timeStamp}}
                </td>
            </tr>
        </thead>
    </table>
</div>

Javascipt区间码

代码语言:javascript
运行
复制
<script>
  window.setInterval(function () {
    $.ajax({
        url: 'api/scans/',
        type: 'Get',
        dataType: 'json',
        success: function (data) {                
        //Something here
        },
        error: function () {
          alert("something failed");
        }
     });

    }, 10000);
</script>

角码

代码语言:javascript
运行
复制
var myApp = angular.module('myApp', []);

myApp.service('dataService', function ($http) {

this.getData = function () {

    return $http({
        method: 'GET',
        url: '/api/scans/'
    });
}
});

myApp.controller('ScansController', function ($scope, dataService) {
$scope.Scans = [];

    dataService.getData().then(function (result) {
        $scope.Scans = result.data;
        console.log(result.data);
    }); 
});
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-06 16:39:03

你需要呆在当前的范围内。

设置$http调用的间隔是有害的。使用成功回调中的$timeout递归调用下一个间隔。

代码语言:javascript
运行
复制
myApp.controller('ScansController', function ($scope, $timeout, dataService) {

  $scope.Scans = [];

  function fetchData(){
    dataService.getData().then(function (result) {
      $scope.Scans = result.data;
      $timeout(function(){ fetchData(); },10000);
    });   
  }

  fetchData();
});
票数 2
EN

Stack Overflow用户

发布于 2016-09-08 18:34:02

至于没有被处理的表刷新,这就是我能够使它工作的方式。我下载并应用了animate.css。然后,我给这个表一个开始类,以便在类加载时将其动画化。然后,我有一个函数,该函数在页面加载时获取数据数组,然后另一个函数每隔一秒钟提取一次数据并进行比较。如果有任何变化,那么类将被重新应用并显示动画。

角槽-重复表

代码语言:javascript
运行
复制
<link href="~/Content/animate.min.css" rel="stylesheet" />
<h1>
Scans
</h1>
<body ng-app="myApp" ng-controller="ScansController" >   
    <table id="scansTable" class="table table-striped">
        <thead>
            <tr>
                <th>ScanId</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Time Stamp</th>
            </tr>
            <tr ng-repeat="scan in Scans" ng-class-odd="'odd'" ng-class-even="'even'" class="animated bounceIn">
                <td>
                    {{scan.scanId}}
                </td>
                <td>
                    {{scan.firstName}}
                </td>
                <td>
                    {{scan.lastName}}
                </td>
                <td>
                    {{scan.timeStamp}}
                </td>
            </tr>
        </thead>
    </table>

角控制器

代码语言:javascript
运行
复制
var myApp = angular.module('myApp', []);

myApp.service('dataService', function ($http) {

this.getData = function () {

    return $http({
        method: 'GET',
        url: '/api/scans/'
    });
}
});

myApp.controller('ScansController', function ($scope, dataService, $timeout) {

$scope.Scans = [];
$scope.NewScans = [];

function fetchData() {
    dataService.getData().then(function (result) {

        $scope.Scans = result.data;
        $("#scansTable").removeClass('animated bounceIn');           
    });
}

function fetchNewData() {
    dataService.getData().then(function (result) {

        $scope.NewScans = result.data;

        if ($scope.Scans.length != $scope.NewScans.length)
        {               
            $("#scansTable").addClass('animated bounceIn');
            $scope.Scans = $scope.NewScans              
        }

        $timeout(function () { fetchNewData(); }, 500);
    });
}

fetchData();
fetchNewData();

});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39353522

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档