[英]Using Generators to iterate in AngularJS using ng-repeat not working as expected
AngularJS中的ng-repeat似乎不適用於生成器函數。 它可以在控制器內按預期工作,但是在HTML表達式內使用時,生成器似乎已經終止。 你們能否向一個有角的菜鳥解釋這里發生了什么法術?
function Main($scope) {
$scope.items = function*() {
//console.log(1);
yield 1;
//console.log(2);
yield 2;
return null;
}();
//console.log($scope.items);
//console.log($scope.items.next().value);
//console.log($scope.items.next().value);
}
<div ng-app ng-controller="Main">
<h2>{{items.next()}}</h2>
<div ng-repeat="item in items">
Hi {{item}}
</div>
</div>
為了使生成器函數返回可以迭代的生成器,應使用items()
進行調用, items
只是一個函數。 在這種情況下,這仍然行不通。
Iterables (和發電機是那些)是指與在傳統JS循環特殊處理進行迭代,這涉及到監測generator.next().done
狀態來結束循環。 for...of
循環用於透明地處理可迭代對象。
生成器沒有length
屬性,並且不一定必須是有限的。 ngRepeat
通過使用length
屬性在for
循環中遍歷對象鍵或ngRepeat
數組的數組 ,它不知道iterables / generators。
如果生成器是有限的,則應在將其生成ngRepeat
之前將其轉換為array。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.