角度2 - 动画过渡不起作用

Flo*_*sdG 7 html typescript angular2-animation angular

我需要两种颜色之间的过渡OnClick.现在,这是我的代码:

打字稿

animations: [
    trigger('menubarState', [
        state('false', style({backgroundColor:'#43a047'})),
        state('true', style({backgroundColor:'#333'})),
        transition('false => true', animate('1s')),
        transition('true => false', animate('1s'))
    ])
]

...

export class MenubarComponent {
  menuActive: boolean = false;
  onMenuClick () {
    if (this.menuActive == false) {
      this.menuActive = true;
    } else {
      this.menuActive = false;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

HTML

<li [@menubarState]="menuActive" (click)="onMenuClick()">
      <a><span class="material-icons icon">apps</span></a>
</li>
Run Code Online (Sandbox Code Playgroud)

这确实改变了background-color它应该.然而,这种变化是即时的,而不是过渡.

我使用的是Chrome,最新版本.

Aar*_*uss 9

这让我觉得时间最长,修正了它将我的状态变量从布尔类型改为字符串类型.所以-而不是跟踪truefalse,跟踪'true''false'.Per Angular的文档:

动画状态是您在应用程序代码中定义的字符串值.

将MenubarComponent类更改为:

export class MenubarComponent {
  menuActive: string = 'false';
  onMenuClick () {
    if (this.menuActive === 'false') {
      this.menuActive = 'true';
    } else {
      this.menuActive = 'false';
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我觉得这很愚蠢.布尔运算显然是二元状态的好选择.

更多信息:https://angular.io/guide/animations#states-and-transitions