12

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.

1
  • I didn't think of this use case when I answered the question :D, but that's won't work for the ngIf directive (see my answer). Commented Oct 23, 2016 at 20:07

2 Answers 2

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.

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

1 Comment

Since the new Directive composition API was released, this answer is no longer true. angular.dev/guide/directives/directive-composition-api
3

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.