简体   繁体   English

Angular2 动画不适用于 ngIf

[英]Angular2 animate doesn't work with ngIf

<div *ngIf="clientTable" [@clientTableState]="testAnimate.state" class="clientdata">
  <div class="table-holder">
    <table id="header-fixed"><a (click)="closeClientTable()" class="table-close"><i class="ion-close-circled"></i></a>
      <tr>
        <th colspan="8">{{ clientTableHeader }}</th>
      </tr>
      <tr>
        <th rowspan="2">Bookie</th>
        <th colspan="3">Payment</th>
        <th colspan="3">Bookie</th>
        <th rowspan="2">Balance</th>
      </tr>
    </table>
  </div>
</div> 

@Component({
  templateUrl: 'app/html-templates/bookiedata.html',
  styleUrls: ['css/templates.css'],
  animations: [
    trigger('clientTableState', [
        state('inactive', style({
            opacity: 0
        })),
        state('active', style({
            opacity: 1
        })),
        transition('inactive => active', animate('0.8s ease-in-out')),
        transition('active => inactive', animate('0.8s ease-in-out'))
    ])
  ]
})

Toggling State Method切换状态方法

// declaration
clientTable: boolean = false
testAnimate: any = { state: 'inactive' }

// method
toggleState(){
    this.testAnimate.state == 'inactive' ? this.testAnimate.state = 'active' : this.testAnimate.state = 'inactive'
    this.clientTable = true
}

However, if I remove *ngIf ng2-animation will work.但是,如果我删除 *ngIf ng2-animation将起作用。

Theoretically, ngIf checks and if it's true.理论上,ngIf 会检查它是否为真。 The element would be available on DOM.该元素将在 DOM 上可用。 Based on my understanding, the element should also have the state inactive since it was not triggered.根据我的理解,该元素也应该处于非活动状态,因为它没有被触发。 After clicking the trigger, the element will be available and will have the state of active.单击触发器后,该元素将可用并处于活动状态。

But why it doesn't have any animation?但为什么它没有任何动画?

Is there any work around that will let me use ngIf and animation?是否有任何解决方法可以让我使用 ngIf 和动画?

It should work with this,它应该适用于此,

toggleState(){

    this.clientTable = true          //<<<===changed order.

    this.testAnimate.state == 'inactive' ? this.testAnimate.state = 'active' : this.testAnimate.state = 'inactive'

}

If it doesn't work (workaround)如果它不起作用(解决方法)

toggleState(){

    this.clientTable = true          //<<<===changed order.

    setTimout(()=>{
       this.testAnimate.state == 'inactive' ? this.testAnimate.state = 'active' : this.testAnimate.state = 'inactive'
    },2000)

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM