11

I'm trying to set ngIf on the host element using @HostBinding decorator.

class ListItem {
    @HostBinding('ngIf') active: boolean = false;
}

And I'm getting the error: Can't bind to 'ngIf' since it isn't a known property of 'list-item'.

However I see an answer here which seems to be suggesting this usage.

Community
  • 1
  • 1
parliament
  • 21,544
  • 38
  • 148
  • 238

2 Answers2

14

ngIf is a directive and directives can't be added dynamically. They are only applied if markup added statically to a components template matches the selector.

@HostBinding() only supports class., attr., and style. bindings.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
1

just wrap youur template in <ng-container *ngIf="active"></ng-container> instead of using host binding

Matt
  • 2,096
  • 14
  • 20