我在使用来自API的数据进行动画时遇到了问题,例如,如果我想实现Angular 4的瞬间动画,我需要提供对象长度。
<div [@listAnimation]="objects.length">
<div *ngFor="let object of objects">
{{ object?.title }}
</div>
</div>
问题在于,在我向API发送http请求完成之前,大约少于一秒钟或更长时间,对象的长度值为未定义!
这是我的组件:
objects: {};
constructor(private _api: ApiService ) {}
ngOnInit() {
this._api.getAllBlogPosts()
.subscribe(result => this.objects= result);
}
而交错动画看起来像这样:
import { trigger, state, animate, transition, style , query , stagger , keyframes} from '@angular/animations';
export const listAnimation =
trigger('listAnimation', [
transition('* => *', [
query(':enter' , style({ opacity: 0 }) , {optional: true}),
query(':enter' , stagger( '300ms' , [
animate('1s ease-in' , keyframes([
style({opacity: 0 , transform: 'translateY(-75px)' , offset: 0}),
style({opacity: .5 , transform: 'translateY(35px)' , offset: 0.3}),
style({opacity: 1 , transform: 'translateY(0)' , offset: 1})
]))
]) , {optional: true}),
query(':leave' , stagger( '300ms' , [
animate('1s ease-in' , keyframes([
style({opacity: 1 , transform: 'translateY(0)' , offset: 0}),
style({opacity: .5 , transform: 'translateY(35px)' , offset: 0.3}),
style({opacity: 0 , transform: 'translateY(-75px)' , offset: 1})
]))
]) , {optional: true})
]),
]);
在调用完成后有没有任何方式可以调用或运行动画?