Начиная с Angular 15 появилась новая возможность: хост-директивы.
Давайте начнем с примера: @Component({
selector: '...',
host: {
...
'[class.my-disabled-class]': 'disabled',
'[attr.disabled]': 'disabled || null',
'[attr.aria-disabled]': 'disabled || null',
...
},
template: `...`
})
class MyComponent {
@Input() public disabled = false;
// many other stuffs
}
Как можно улучшить этот код? Первая мысль - создать директиву. @Directive({
selector: '[myDisabledState]',
standalone: true,
host: {
'[attr.disabled]': 'disabled || null',
'[attr.aria-disabled]': 'disabled || null',
'[class.rc-disabled]': 'disabled',
},
})
export class DisabledStateDirective {
@Input() public disabled = false;
}
И теперь код можно упростить до такого: @Component({
selector: '...',
host: {
...
},
hostDirectives: [{
directive: DisabledStateDirective,
inputs: ['disabled'],
}],
template: `...`
})
class MyComponent {
// many other stuffs
}
В таком случае код читается проще, и соблюдается