【发布时间】:2018-04-06 06:09:24
【问题描述】:
我对来自 api 的数据的动画有疑问,例如,如果我想实现 4 角交错动画,我需要提供 objects.lenght
<div [@listAnimation]="objects.length">
<div *ngFor="let object of objects">
{{ object?.title }}
</div>
</div>
问题是 objects.lenght 的值在我对 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})
]),
]);
调用完成后有什么方法可以调用或者运行动画吗?
【问题讨论】:
-
我不知道 Angular 4,但在 1.x 中,我会将其包装在
<div>或<span>和ng-show或ng-if中,因为objects不是-null -
@Mawg 谢谢,解决了我的问题
-
然后我将评论复制到一个答案中,您可以接受它,以帮助其他人。欢迎加入并很高兴能提供帮助
标签: angular animation angular-animations