-
Notifications
You must be signed in to change notification settings - Fork 26.6k
Description
As far as I can tell there is no good way to make a decorator conditionally applied. My experience is with Angular 2 in Dart, for an internal app (zny@). This is a feature request for some template syntax or something similar (maybe an automatically generated input for all directives like applyDirectiveName?) to allow for a decorator to be conditionally applied to an element.
There are several possible workarounds in lieu of this feature, all of which are significantly bad:
- Duplicate the code in the template, and have one version with the decorator, one without. This is bad because it requires duplication, and if there's content inside the decorated element it could be a lot of duplication.
<div *ngIf="condition" decorator>lots-of-content-goes-here</div>
<div *ngIf="!condition">lots-of-content-goes-here</div>
- Add an input to the decorator that turns it on or off. This is bad because it requires an extraneous input just to say "don't do anything at all" and can be confused with similar inputs (e.g. a button decorator that has a disabled input). Also bad because you may be using a decorator from a library or some such where you cannot easily add this yourself.
<div decorator decorator-input="condition">content</div>
- Create a base class for the decorator and have it share most stuff, where one impl takes a conditional input and the other doesn't, this is impossible to do well because you cannot share the properties passed to the decorator: Angular2 Dart: No way to share annotation properties #6575
- Create a component that wraps the content twice, once with decorator and once without, and then ngIf between the two based on the conditional. This is impossible because you cannot project the same content twice into an ng-content (see e.g. this comment for some discussion, I didn't open an issue because this appears to be by design: problem with ng-content within a ng-for #4631 (comment) )
`@Component(selector: 'conditional-decorator-wrapper',
directives: const [ExampleDecorator],
template: '<div *ngIf="condition" exampleDecorator>