I have a custom button component with a structure like this one (simplified version of code for readability purposes):
HTML:
<button type="button" class="btn btn-primary">
<ng-content select="[appButtonIcon]"></ng-content>
<i *ngIf="iconClass && !customIcon" class="fa {{iconClass}}"></i>
{{isIconButton ? '' : text}}
</button>
TS:
@ContentChild(ButtonIconDirective, { static: true }) customIcon: ButtonIconDirective;
And it works fine. Note, please, the line <ng-content select="[appButtonIcon]"></ng-content>. I created a custom directive appButtonIcon so the user can add a custom icon, like SVG content, for example. This is working properly, I can do both:
1
<custom-button text="Test" (clicked)="onClick()">
<svg appButtonIcon ...>
<path ... />
</svg>
</custom-button>
2
<custom-button text="Test" (clicked)="onClick()" iconClass="fa-folder">
</custom-button>
And they work properly. The problem starts when I add this custom button as part of another custom component, my custom datepicker toggle. When I do so, only of the options will work. I can only use a regular icon class or always use a custom icon with the appButtonIcon directive:
Custom datepicker toggle HTML
Ideal(?):
<app-custom-button (click)="open($event)" iconClass="fa-calendar" isIconButton="true">
<ng-content select="[appButtonIcon]"></ng-content>
</app-custom-button>
But it doesn't work, app-custom-button can never read the content I add inside the custom datepicker toggle with the directive appButtonIcon.
Version only working if custom icons are set:
<app-custom-button (click)="open($event)" iconClass="fa-calendar" isIconButton="true">
<ng-container appButtonIcon>
<ng-content select="[appButtonIcon]"></ng-content>
</ng-container>
</app-custom-button>
The problem with this approach is, that, since I had to manually add the ng-container with the appButtonIcon directive, it's always going to be read by the button (obviously), even when there's no content set by the user of the custom datepicker toggle. But if I remove that, the inner content is never read, and the fa-calendar class always used. Is there a way to fix that?
Please take a look at StackBlitz for a "working" example: https://stackblitz.com/edit/angular-ymxtmv