6

我写了 plunk 来说明我的问题:LINK

我需要为父组件制作动画,同时我想在子组件中制作一些动画。似乎角度正在阻止子组件上的动画,然后在父动画结束后简单地跳转状态而没有任何过渡。

有没有办法让动画并行工作,或者至少在不使用回调的情况下进行链接?

@Component({
  selector: 'outer',
  template: `
    <div [@state]="state" (mouseenter)="state='wide'" (mouseleave)="state='narrow'" style="background-color: red;">
      <inner [stateInner]="state"></inner>
    </div>`,
  animations:[
    trigger('state', [
      state('narrow', style({
        width: '100px'
      })),
      state('wide', style({
        width: '400px'
      })),
      transition('* => *', animate('500ms'))
    ])  
  ]
})
export class Outer {
  public state: string = 'narrow';
  constructor() {
  }
}


@Component({
  selector: 'inner',
  template: `
    <div [@stateInner]="stateInner">
      <h2>Hello</h2>
    </div>`,
  animations:[
    trigger('stateInner', [
      state('narrow', style({
        height: '100px'
      })),
      state('wide', style({
        height: '400px'
      })),
      transition('* => *', animate('500ms'))
    ])  
  ]
})
export class Inner {
  @Input() stateInner: string = 'narrow';
  constructor() {
  }
}
4

2 回答 2

4

我想说,使用回调是为将来的代码处理这个问题最佳方式,但如果你只需要让它工作,诀窍是使用OnChangesSimpleChanges和 setTimeout()。

使用 Plunker来展示它是如何工作的,以及代码中的内部 div 主要变化:

进口

import {Component, Input, OnChanges, SimpleChanges} from '@angular/core'

模板

  template: `
    <div [@stateInner]="localChange">
      <h2>Hello</h2>
    </div>

类导出

  localChange = 'narrow';

  ngOnChanges( changes: SimpleChanges ) {
    console.log(changes)
    setTimeout( () => this.localChange = changes.stateInner.currentValue, 500);
  }
于 2017-09-12T14:17:26.870 回答
3

您可以在没有事件和超时的情况下同时运行父动画和子动画,animateChild()可以帮助我们。这是父动画描述:

animations: [
    trigger('state', [
        state('narrow', style({
            width: '100px'
        })),
        state('wide', style({
            width: '400px'
        })),
        transition('narrow => wide', [
            style({
                width: '100px'
            }),
            group([
                animate('500ms', style({
                    width: '400px'
                })),
                query('@stateInner', [
                    animateChild()
                ])
            ])
        ]),
        transition('wide => narrow', [
            style({
                width: '400px'
            }),
            group([
                animate('500ms', style({
                    width: '100px'
                })),
                query('@stateInner', [
                    animateChild()
                ])
            ])
        ])
    ])
]

group() - 并行运行多个动画,这是文档中的一个示例

query() - 查找子动画

animateChild() - 执行子动画

正如您可能注意到的那样,此解决方案的缺点是,我分别描述了前向后向父转换和样式,否则父状态由于某种原因无法正确设置动画。是我关于这个问题的问题。

于 2018-08-07T10:32:30.137 回答