35

i added an animation to the host via

@Component({
   ....,
   animations: [
      trigger('slideIn', [
          ...
      ])
   ],
   host: {
      '[@animation]': 'condition'
   }
}

which worked well, on compilation i was told this is deprecated and i should use @HostBinding ...

@HostBinding('[@animation]') get slideIn() {
   return condition;
}

which throws me an error

Can't bind to '[@animation' since it isn't a known property of 'my-component-selector'.

but i cannot add an animation into my module.. what can i do ?

0

2 Answers 2

52

The square brackets are not necessary with @HostBinding()

@HostBinding('@slideIn') get slideIn() {

There are two decorators @HostBinding() and @HostListener() therefore the distinction between () and [] isn't necessary, while it is when host: [...] is used.

Sign up to request clarification or add additional context in comments.

Comments

18

I'm writing this answer because I struggled a little bit with the syntax and I like examples for dummies, but Günter's answer is correct.

What I had to do:

    @Component({
        selector: 'app-sidenav',
        templateUrl: './sidenav.component.html',
        styleUrls: ['./sidenav.component.scss'],
        animations: [
            trigger('toggleDrawer', [
                state('closed', style({
                    transform: 'translateX(0)',
                    'box-shadow': '0px 3px 6px 1px rgba(0, 0, 0, 0.6)'
                })),
                state('opened', style({
                    transform: 'translateX(80vw)'
                })),
                transition('closed <=> opened', animate(300))
            ])
        ]
    })
    export class SidenavComponent implements OnInit {

        private state: 'opened' | 'closed' = 'closed';

        // binds the animation to the host component
        @HostBinding('@toggleDrawer') get getToggleDrawer(): string {
            return this.state === 'closed' ? 'opened' : 'closed';
        }

        constructor() { }

        ngOnInit(): void {
        }

        // toggle drawer
        toggle(): void {
            this.state = this.state === 'closed' ? 'opened' : 'closed';
        }

        // opens drawer
        open(): void {
            this.state = 'opened';
        }

        // closes drawer
        close(): void {
            this.state = 'closed';
        }

    }

3 Comments

Anyone knows, how to bind an (@toggleDrawer.done) (callback on animation end) to the host?
@FrankNocke Have you tried @HostListener('@toggleDrawer.done', ['$event']) ? Might work.
@tinnick wow, it's really working like this! @HostListener('@transformPanel.done', ['$event']) thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.