【问题标题】:Angular animation not triggering角度动画未触发
【发布时间】:2019-11-03 17:23:00
【问题描述】:

我正在尝试用 angular 实现一个简单的动画。单击按钮时,我将showState 的状态从shown 更改。由于我使用 *ngIf 我在动画中使用了 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


    【解决方案1】:

    所以,我自己想通了。我失踪了:

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

    在 appModule.ts 中

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

    【讨论】:

      【解决方案2】:

      您不应同时使用[@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;
      }
      

      【讨论】:

        猜你喜欢
        • 2019-08-14
        • 2021-05-17
        • 2019-03-01
        • 2021-10-14
        • 1970-01-01
        • 1970-01-01
        • 2020-03-18
        • 2019-01-31
        • 1970-01-01
        相关资源
        最近更新 更多