如何在组件内部手动触发动画?
How to trigger animation manually inside the component?
在 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 => *
,您可以尝试提供特定的 names/booleans,例如 false => true
,并将其绑定到一个成员变量。尝试以下
行-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;
public fadeInStart = false; // <-- hide chart by default
constructor(
private lineChartService: LineChartService,
private elRef: ElementRef,
) { }
ngAfterViewInit() {
this.lineChartService.makeGraph(
this.curveData,
this.elRef.nativeElement,
this.graphSize,
);
this.fadeInStart = true; // <-- show chart here
}
}
行-chart.component.html
<div [@fade]="fadeInStart">
<!-- chart -->
</div>
常春藤更新 (03/15/21)
在 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 => *
,您可以尝试提供特定的 names/booleans,例如 false => true
,并将其绑定到一个成员变量。尝试以下
行-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;
public fadeInStart = false; // <-- hide chart by default
constructor(
private lineChartService: LineChartService,
private elRef: ElementRef,
) { }
ngAfterViewInit() {
this.lineChartService.makeGraph(
this.curveData,
this.elRef.nativeElement,
this.graphSize,
);
this.fadeInStart = true; // <-- show chart here
}
}
行-chart.component.html
<div [@fade]="fadeInStart">
<!-- chart -->
</div>
常春藤更新 (03/15/21)