在Angular 9 animations中,如何从组件本身触发动画?我假设我会在组件本身中手动执行此操作,因为它会跟踪创建图形时的状态。与使用模板表达式相反,在该模板表达式中,父级将通过数据绑定和主机属性来跟踪状态。
<div class="chart-body">
<div *ngFor="let chart of charts | async | daysFilter:7" class="last-seven-days-body">
<line-chart
[curve-data]="chart"
graph-size="med"></line-chart>
</div>
</div>
@Component({
selector: 'line-chart',
templateUrl: './line-chart.component.html',
styleUrls: ['./line-chart.component.css'],
animations: [
trigger('fadeIn', [
transition('void => *', [
style({ opacity: 0 }),
animate(2000, style({opacity: 1}))
])
])
],
})
export class LineChartComponent {
@Input('curve-data') curveData: Array<object>;
@Input('graph-size') graphSize: String;
constructor(
private lineChartService: LineChartService,
private elRef: ElementRef,
private fadeInStart: Boolean,
) { }
ngAfterViewInit() {
this.lineChartService.makeGraph(
this.curveData,
this.elRef.nativeElement,
this.graphSize,
);
this.fadeInStart = true; //AFTER GRAPH IS MADE, TURN ON FADE IN ANIMATION HERE
}
}
[而不是使用转换void => *
,您可以尝试提供特定的名称/布尔值,例如false => true
,并将其绑定到成员变量。尝试以下操作
line-chart.component.ts
@Component({
selector: 'line-chart',
templateUrl: './line-chart.component.html',
styleUrls: ['./line-chart.component.css'],
animations: [
trigger('fade', [
state('false', style({ opacity: 0 })),
state('true', style({ opacity: 1 })),
transition('false => true', animate('2000ms ease-in')),
transition('true => false', animate('2000ms ease-out'))
]),
],
})
export class LineChartComponent {
@Input('curve-data') curveData: Array<object>;
@Input('graph-size') graphSize: String;
constructor(
private lineChartService: LineChartService,
private elRef: ElementRef,
private fadeInStart: Boolean,
) { }
ngAfterViewInit() {
this.lineChartService.makeGraph(
this.curveData,
this.elRef.nativeElement,
this.graphSize,
);
this.fadeInStart = true; //AFTER GRAPH IS MADE, TURN ON FADE IN ANIMATION HERE
}
}
line-chart.component.html
<div [@fade]="fadeInStart">
<!-- chart -->
</div>