1

I read that its better to use @HostBinding instead of :host. So i think about change my component.ts

@Component({
    host: {
        'class': 'cover',
        '[class.uploading]': 'uploadProgress > 0',
    }
})

This works fine, but when i change it to:

export class Cover {
    @HostBinding('class') classes = 'mark6-cover';
    @HostBinding('[class.uploading]') uploadProgress > 0;

    @Input() uploadProgress = null;
}

i get errors and nothing works. What i did wrong here? And how can i make the variable uploadProgress to a observable?

1 Answer 1

2

A counterpart to '[class.uploading]': 'uploadProgress > 0' would be:

@HostBinding('class.uploading')
@Input() 
uploadProgress = null;

uploadProgress input can be set asynchronously from observable subscription or elsewhere.

uploading class will be triggered for truthy uploadProgress, which is likely the expected behaviour.

In case the condition is different (e.g. input value can be negative, while a class should be triggered for positive values only), extra property can be added:

@Input() 
uploadProgress = null;

@HostBinding('class.uploading')
get isUploading() {
  return this.uploadProgress > 0;
}

If the condition is complex, it's preferable for a component to have OnPush change detection strategy.

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

7 Comments

Thanks, but this overwrite the @Input() uploadProgress = null; And why i cant set Operators like > there? i tryed now !== null but this also dont work on this place... i need to set the class only if there is any activity on uploadProgress... And i need to remove it again when the progress is done. Can it be that this is simply not possible with @HostBinding?
Sure, @Input is needed there too. I added some explanation. Hope this helps.
I thank you so much, but i cant make it work. i tryed with your bottom code example, but the class uploading is still missing when i add a dummy value from outside (like 33). And i use allready changeDetection: ChangeDetectionStrategy.OnPush
Thanks, now it works, i dont know why it yesterday dont. Maybe to tired ;) So you can confirm that @HostBinding is more dynamic than :host?
That's a matter of taste. I always use HostBinding, it's more expressive. You can also inherit it in child classes, IIRC.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.