I would like a way to allow using the host property in component/directive decorators as long as the keys are being set to string values. For example, this code:
@Component({
selector: 'my-component',
host: {
class: 'some-base-class some-modifier-class other-modifier-class'
}
})
class MyComponent {}
seems more readable than:
@Component({
selector: 'my-component'
})
class MyComponent {
@HostBinding('class.some-base-class')
someBaseClass = true;
@HostBinding('class.some-modifier-class')
someModifierClass = true;
@HostBinding('class.other-modifier-class')
otherModifierClass = true;
}
@eppsilon would you open an issue for this in the angular.io repository. Here's a template issue https://github.com/angular/angular.io/issues/1301.
Closing it for now. @eppsilon let me know if the style guidelines change if I don't notice.
@mgechev Seeing that the styleguide issue is closed, would there be any chance we could make this configurable in some way? By adding some 'allow-constant-values' setting to the rule?
I'd prefer to collect some more votes here.
It's super annoying to have to create a boolean set to true, just to bind a static class to a component. It's so much cleaner to use the host property for a static string binding. Really feels like I'm fighting the compiler to do something "the right way" and the compiler is forcing me to do it "the wrong way".
I disabled this lint rule since there is 0 argument for avoiding host when binding literals. I often bind tabindex or aria-* o role up there. It makes much more sense than polluting the class with public stuff all over the place just to bind to the host. If we could have what the issue suggests, it would be useful again.
I think the option should be considered. I understand this is prefer, but in the case I have a value I know it won't ever change, having Angular checking if it did change in the check detection cycle seems like a waste of resources for me.
Most helpful comment
I disabled this lint rule since there is 0 argument for avoiding
hostwhen binding literals. I often bindtabindexoraria-*oroleup there. It makes much more sense than polluting the class with public stuff all over the place just to bind to the host. If we could have what the issue suggests, it would be useful again.