这是 stackbliz 的解决方案链接到您的问题,我刚刚使用了 Angular 动画的禁用属性。如果 flag 变量为 true,则动画将被禁用,如果为 false,则启用动画,我们在页面初始化后加载所有内容,以便动画的触发器仅适用于推送操作。
Stackbliz
Reference Link
ngAfterViewInit Reference link
编辑:为避免控制台错误ExpressionChangedAfterItHasBeenCheckedError,请在添加项目时启用动画,如下所示。甚至stackbliz代码也相应更新了,以供参考。
addItem() {
if (this.flag) {
// Enabling Animation
this.flag = !this.flag;
}
this.items.push("another item");
}
app.component.ts
import { Component } from "@angular/core";
import {
animate,
animateChild,
group,
query,
style,
transition,
trigger,
state
} from "@angular/animations";
@Component({
selector: "my-app",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"],
animations: [
trigger("myTrigger", [
state(
"fadeInFlash",
style({
opacity: "1"
})
),
transition("void => *", [
style({ opacity: "0", transform: "translateY(20px)" }),
animate("500ms")
])
])
]
})
export class AppComponent {
flag: boolean = true;
items = ["item 1", "item 2", "item 3"];
state: string = "fadeInFlash";
addItem() {
if (this.flag) {
this.flag = !this.flag;
}
this.items.push("another item");
}
}
app.component.html
<p>
Start editing to see some magic happen :)
</p>
<p>
<button (click)="addItem()">Add Item</button>
</p>
<ul>
<li *ngFor="let item of items" [@.disabled]="flag" [@myTrigger]='state'>
{{item}}</li>
</ul>