[英]ngFor crashes when adding new items to collection after animation in Nativescript Angular
我使用ngFor將視圖放在另一個之上。 卡片種類:
<GridLayout rows="*">
<GridLayout row="0" *ngFor="let image of images; trackBy: trackByList"
verticalAlignment="center" horizontalAlignment="center"
[width]="width" [height]="width" (loaded)="itemLoaded($event)"
[rotate]="getRotationAngle(image)" [style.z-index]="image.zIndex">
<Image [src]="image.path" [id]="image.zIndex" stretch="aspectFill"
[width]="width" [height]="width">
</Image>
</GridLayout>
</GridLayout>
itemLoaded(event) {
let grid: any = event.object;
grid.on(GestureTypes.swipe, (args: SwipeGestureEventData) => {
if (this.animationIsRunning) {
return;
}
let x = this.width * 1.5;
let rotate = 45;
if (args.direction == SwipeDirection.right) { }
else if (args.direction == SwipeDirection.left) {
x *= -2;
rotate *= -1;
}
else {
return;
}
this.animationIsRunning = true;
grid.animate({ translate: { x: x, y: 0 }, opacity: 0, rotate: rotate, duration: 500 })
.then(() => {
setTimeout(() => {
this.swipeFinished();
}, 10);
})
.catch((e) => {
console.log(e.message);
})
})
}
//
private swipeFinished() {
this.swipedImages++;
if (this.images.length - this.swipedImages <= 2) {
console.log("Requesting next page");
this.addNextPage();
console.log("Next page loaded. Refreshing changes.");
this.cdr.detectChanges();
this.animationIsRunning = false;
}
else {
this.animationIsRunning = false;
}
}
trackByList(index, item: ImageObj) {
return item.zIndex;
}
我正在使用頁面機制從服務器加載項目。 每次5個。 加載下一頁並將新元素插入項目並調用this.cdr.detectChanges() 后,我崩潰了”
未處理的承諾拒絕:無法讀取 null 的屬性“銷毀”; 區域: ; 任務: Promise.then ; 值:類型錯誤:無法讀取 null 的屬性“已銷毀”
查看您的代碼后,我發現崩潰的兩個主要原因。
這是更新的操場,崩潰似乎在我這邊得到了修復。 但老實說,它仍然可以改進。 我建議您查看 Jen Looper 撰寫的關於Tinder Style Cards with NativeScript Angular的博客文章,這可能會幫助您改進設計。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.