AngularJS$带$timeout的有状态过滤器陷入循环
AngularJS $stateful filter with $timeout stuck looping
我正试图用AngularJS创建一个有状态过滤器,以最终调用一个异步返回数据的服务。为了测试这一点,我只使用了一个简单的$timeout函数来console.log一个字符串。然而,这个console.log只是无限地运行。
export default angular.module('components.filters.combine-name', [])
.filter('combineName', ['$timeout', function ($timeout) {
function combineName(input) {
$timeout(function () {
console.log('test');
}, 1000);
return input.firstName + ' ' + input.lastName;
}
combineName.$stateful = true;
return combineName;
}]);
html
<table class="table table-bordered table-striped table-responsive">
<thead>
<tr>
<th>
<div class="th">
Name
</div>
</th>
<th>
<div class="th">
Status
</div>
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="result in vm.results">
<td>{{result | combineName}}</td>
<td>{{result.status}}</td>
</tr>
</tbody>
</table>
控制器:
this.results = [
{
firstName: 'Johnny',
lastName: 'Utah',
status: 'Active'
},
{
firstName: 'Richard',
lastName: 'Reynolds',
status: 'Inactive'
},
{
firstName: 'Randy',
lastName: 'Johnson',
status: 'Active'
}
];
使用$timeout,您基本上打开循环,它会一直这样做,除非您告诉它停止。
在循环中使用:$timeout.cancel();
相关文章:
- jQuery:循环一个具有不同超时值的循环
- 在循环中分配json值时,值被覆盖
- 如何在下面的ES6循环中获得前面的文本
- 为什么“;未定义的“;在JavaScript中结束循环
- Javascript循环不会自我更新
- 如何使用jquery处理php循环通过元素
- 而循环只设置php中输入字段中的第一个值
- 循环遍历数组中的特定索引
- Javascript返回值只在循环中返回一次
- 按照选项卡索引的顺序循环一个jQuery选择
- 数组过滤器导致无限$digest循环
- 在 Javascript 中循环使用参数的过滤器
- 使用for/in循环的javascript过滤器
- AngularJS$带$timeout的有状态过滤器陷入循环
- 如何在循环中.js延迟链接多个过滤器(从数组)
- javascript在数组过滤器中嵌套for循环
- 使用过滤器和循环从数组中删除多个值
- AngularJS自定义过滤器,对过滤器函数进行无限循环
- 如何在过滤器方法的回调函数中使用for循环
- 如何通过使用选择将D3选区划分为组.循环中的过滤器方法