Angular 2 - 动画显示/隐藏不适用于组件标记

imr*_*ung 3 html css angular2-animation angular

我正在研究用于显示和隐藏组件的动画.

我的Plunker:https://embed.plnkr.co/BxT8SKBq8JwuPP16FDu4/

<animation [@toggleHeight]="isShow"></animation>
<button (click)="isShow = (isShow==='show')? 'hide' : 'show'">Toggle</button>
<calc></calc>
Run Code Online (Sandbox Code Playgroud)

当点击按钮时,我希望"动画"div消失,但事实并非如此."动画"组件内的内容仍然显示.

难道我做错了什么?任何帮助都会很棒.先感谢您.

Dmi*_*try 6

问题是您正在尝试为自定义元素设置动画.它没有display在CSS中定义的模式.如果你添加animation { display: block; }到你的CSS它将工作正常.您可以在其父组件中执行此操作

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  animations: [
     trigger('toggleHeight', [
            state('hide', style({
                height: '0px',
                opacity: '0',
                overflow: 'hidden',
                // display: 'none'
            })),
            state('show', style({
                height: '*',
                opacity: '1',
                // display: 'block'
            })),
            transition('hide => show', animate('200ms ease-in')),
            transition('show => hide', animate('200ms ease-out'))
        ])
    ],
    styles: [`
        animation { display: block; }
    `]
})
Run Code Online (Sandbox Code Playgroud)

或者您可以将其添加到某个全局CSS文件中.