[英]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.