Javascript Angular在短时间内装载两次物品

Javascript Angular在短时间内装载两次物品,javascript,angularjs,Javascript,Angularjs,它加载正确项目的唯一时间是在第一页加载时。然后,在每次刷新时,它会加载两次项目,持续半秒,就像您在GIF中看到的那样 我的代码非常简单,贴上去感觉不对,但我会的。我在表上尝试了ng-clope,但没有任何改变。知道它为什么这样做吗?我怎样才能让它停止 var RefreshData = function () { Machine.getAllMachines().then(function (response) { machineList.allM

它加载正确项目的唯一时间是在第一页加载时。然后,在每次刷新时,它会加载两次项目,持续半秒,就像您在GIF中看到的那样

我的代码非常简单,贴上去感觉不对,但我会的。我在
上尝试了
ng-clope
,但没有任何改变。知道它为什么这样做吗?我怎样才能让它停止

    var RefreshData = function () {
        Machine.getAllMachines().then(function (response) {
            machineList.allMachines = response.data
            console.log('RefreshData 6 sec', machineList.allMachines)
        })
        setTimeout(RefreshData, 6000)
    }
    RefreshData()


<table class="table table-striped table-hover table-bordered">
    <thead>
        <tr>
            <th>Ip</th>
            <th>Status</th>
            <th>Department</th>
            <th> Processor</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="item in machineList.allMachines">
            <td>{{item.Ip}}</td>
            <td>ONLINE</td>
            <td>{{item.Department}}</td>
            <td>{{item.Processor}}</td>
            <td>Edit</td>
        </tr>
    </tbody>
</table>
var RefreshData=函数(){
Machine.getAllMachines().then(函数(响应){
machineList.allMachines=response.data
console.log('RefreshData 6秒',machineList.allMachines)
})
设置超时(刷新数据,6000)
}
刷新数据()
知识产权
地位
部门
处理器
行动
{{item.Ip}}
在线 的
{{项目.部门}
{{item.Processor}
编辑

更新: 通过单击按钮重新创建循环,效果相同。所以超时不是问题所在


通过每6秒递归调用RefreshData方法,基本上创建了一个无限循环。首先使用RefreshData()调用该方法,然后从方法体中使用setTimeout(RefreshData,6000),它将在6000毫秒后调用RefreshData

如果您的目的是在异步检索数据后触发摘要循环,那么您的代码应该如下所示:

var RefreshData = function () {
    Machine.getAllMachines().then(function (response) {
        $timeout(function() { machineList.allMachines = response.data; });
    });       
};
RefreshData();
请确保插入$timeout提供程序。

尝试此代码

      var RefreshData = function () {
            machineList.allMachines = [];
            Machine.getAllMachines().then(function (response) {
                machineList.allMachines = response.data
                console.log('RefreshData 6 sec', machineList.allMachines)
            })
            setTimeout(RefreshData, 6000)
        }
        RefreshData()

我试图从零开始以多种方式重现故障,但失败了。所以我决定将我的应用程序撕成碎片,直到它停止运行。
所以,在把它剥离到骨骼之后,我开始剪切类,结果是Bootstrap
table hover
它正在做这个lol xD!这似乎有点奇怪,但这是我最后改变的唯一一件事,现在效果很好

因此,如果有人遇到此问题,请尝试删除表悬停


您使用的是
$scope.$watch
吗?尝试使用角度$timeout而不是
setTimeout
,并将其移动到
then()中callback@MaxPaymar不,我使用的是
var machineList=this
,没有$watch@charlietfl在
then()
回调中尝试了
$timeout(RefreshData,6000))
,并且没有变化。如果添加machineList.allMachines={};作为Machine.getAllMachines()中的第一个命令。然后(函数(响应){?这解决了问题吗?尝试过了,也是一样。我添加了一个关于这个问题的更新,但与您的答案无关。使用setTimeout通常不是一个好主意。但是在OP的问题中,setTimeout并没有引起任何问题(通过查看共享代码至少看不到)。正如@charlietfl所提到的,摘要周期是通过使用$q触发的(我猜他使用的是$q,但不能确定)。您可以尝试一下