Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 Use animations from outside a component

I have quickly created a simple component

@Component({
  selector: 'saved-overlay',
  templateUrl: './saved-overlay.html',
  exportAs: 'saved-overlay',
  animations: [
    trigger('inOut', [
      transition('void => *', [
        animate("300ms ease-out", keyframes([
          style({opacity: 0}),
          style({opacity: 1})
        ]))
      ]),
      transition('* => void', [
        animate("300ms ease-out", keyframes([
          style({opacity: 1}),
          style({opacity: 0})
        ]))
      ])
    ])
  ]
})
export class SavedOverlayComponent {
}

and I call it in my template this way :

<saved-overlay #myOverlay='saved-overlay'></saved-overlay>

Is there anyway to call the inOut animation from outside my component (i.e. in the template where I reference this component). What I would like ideally would be to use this animation on my component itself :

<saved-overlay #myOverlay='saved-overlay' [@inOut]></saved-overlay>

However, it does trigger an error saying that my inOut animation isn't defined.

like image 321
Scipion Avatar asked May 17 '26 06:05

Scipion


1 Answers

You can use @HostBinding for this:

@Component({
  selector: 'saved-overlay',
  templateUrl: './saved-overlay.html',
  exportAs: 'saved-overlay',
  animations: [
    trigger('inOut', [
      transition('void => *', [
        animate("300ms ease-out", keyframes([
          style({opacity: 0}),
          style({opacity: 1})
        ]))
      ]),
      transition('* => void', [
        animate("300ms ease-out", keyframes([
          style({opacity: 1}),
          style({opacity: 0})
        ]))
      ])
    ])
  ]
})
export class SavedOverlayComponent {
  @HostBinding("@InOut")
  foo:any;
}

then no need to bind to anything or specify it in the template :

<saved-overlay #myOverlay='saved-overlay'></saved-overlay>

Note that I use a random property because we actually don't care about it, as you use the special states (* and void), so you don't need to store anything inside this property, and name it as you like ...

like image 192
n00dl3 Avatar answered May 18 '26 21:05

n00dl3



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!