Как вручную запустить анимацию внутри компонента? - PullRequest
0 голосов
/ 16 июня 2020

В Angular 9 анимациях , как мне запустить анимацию из самого компонента? Полагаю, я бы сделал это вручную в самом компоненте, поскольку он отслеживает состояние создания графа. В отличие от использования выражения шаблона, в котором родитель будет отслеживать состояние с помощью привязки данных и свойства хоста.

<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
  }     
}  

1 Ответ

2 голосов
/ 16 июня 2020

Вместо использования перехода void => * вы можете попытаться дать определенные c имена / логические значения, такие как 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;

  private 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
  }     
}

line-chart.component. html

<div [@fade]="fadeInStart">
  <!-- chart -->
</div>
...