不触发角动画

问题描述 投票:0回答:2

我正在尝试实现一个简单的带有角度的动画。单击按钮后,我将showState的状态从更改为shown。由于我正在使用* ng如果我在动画中使用了void关键字,但它无法正常工作。

STACKBLITZ

CSS

p {
  border: 1px solid black;
  background-color: lightblue;
  padding: 10px;
}

app.component.ts

import { showStateTrigger } from './animations';
import { Component } from "@angular/core";

@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.scss"],
  animations: [
    showStateTrigger
  ]
})
export class AppComponent {
  isShown = false;
}

HTML

<button (click)="isShown = !isShown">Toggle Element</button>
<p [@showState]="isShown ? 'shown' : 'notShown'" *ngIf="isShown"> You can see me now!</p>

Animations.ts

从“ @ angular / animations”导入{状态,样式,过渡,触发器,动画};

export const showStateTrigger = trigger(“ showState”,[

  transition('void => shown', [
    style({
      opacity: 0
    }),
    animate(2000, style({
      opacity: 1
    }))
  ])

]);
javascript css angular animation angular-animations
2个回答
0
投票

所以,我自己弄清楚了。我不见了:

import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

在appModule.ts中

奇怪的是,Angle没有抱怨它。没有错误。没有警告。


0
投票

您不应该同时使用[@showState]="isShown ? 'shown' : 'notShown'"*ngIf="isShown。尤其是在notWhosn不是注册状态时。

您的代码应如下所示:

@Component({
  selector: 'app-root',
  template: `
     <button (click)="isShown = !isShown">Toggle Element</button>
     <p @enterAnimation *ngIf="isShown"> You can see me now!</p>`
  ,
  animations: [
    trigger(
      'enterAnimation', [
      transition(':enter', [
        style({ opacity: 0 }),
        animate('500ms', style({ opacity: 1 }))
      ]),
      transition(':leave', [
        style({ opacity: 1 }),
        animate('500ms', style({ opacity: 0 }))
      ])
    ]
    )
  ],
})
export class AppComponent {
  isShown = false;
}
© www.soinside.com 2019 - 2024. All rights reserved.