I am using Material 2 to add md-raised-button
. I want to apply this directive only if certain condition becomes true.
For example:
<
As already noted this does not appear to be possible. One thing that can be used to at least prevent some duplication is ng-template
. This allows you to extract the content of the element affected by the ngIf
branching.
If you for example want to create a hierarchical menu component using Angular Material:
<!-- Button contents -->
<ng-template #contentTemplate>
<mat-icon *ngIf="item.icon != null">{{ item.icon }}</mat-icon>
{{ item.label }}
</ng-template>
<!-- Leaf button -->
<button *ngIf="item.children == null" mat-menu-item
(click)="executeCommand()"
[disabled]="enabled == false">
<ng-container *ngTemplateOutlet="contentTemplate"></ng-container>
</button>
<!-- Node button -->
<ng-container *ngIf="item.children != null">
<button mat-menu-item
[matMenuTriggerFor]="subMenu">
<ng-container *ngTemplateOutlet="contentTemplate"></ng-container>
</button>
<mat-menu #subMenu="matMenu">
<menu-item *ngFor="let child of item.children" [item]="child"></menu-item>
</mat-menu>
</ng-container>
Here the conditionally applied directive is matMenuTriggerFor
, which should only be applied to menu items with children. The contents of the button are inserted in both places via ngTemplateOutlet
.
I got another idea about what you could do.
You could store the html you want replaced in a variable as a string and then add / remove the directive from it as you wish, using the bypassSecurityTrustHtml
method of the DomSanitizer.
I doesn't result in a clean solution but at least you don't need to repeat the code.
As at 18th Jan 2019,
This is how I added a directive conditionally in Angular 5 and above. I needed to change the color of the <app-nav>
component based on darkMode
. If the page was in dark mode or not.
This worked for me:
<app-nav [color]="darkMode ? 'orange':'green'"></app-nav>
I hope this helps someone.
EDIT
This changes the value of an attribute (color) based on a condition. It just happens that the color is defined using a directive. So anyone reading this please do not get confused, this is not applying a directive conditionally (ie. which means adding or removing a directive to the dom based on a condition)
Use NgClass
[ngClass]="{ 'mat-raised-button': trueCondition }"
example of true condition:
this.element === 'Today'
or a boolean function
getTruth()
full example:
<button [ngClass]="{ 'mat-raised-button': trueCondition }">TEXT</button>
If you want a default class:
<button [ngClass]="{ 'mat-raised-button': trueCondition, 'default-class': !trueCondition }">TEXT</button>
Currently, there is NO
way to conditionally apply a directive to a component.This is not supported.The components which you have created can be added or removed conditionally.
There is already an issue created for the same with angular2, so it should be the case with angular4 aswell.
Alternatively you can go for the option with ng-if
<button ngIf="!condition"></button>
<button ngIf="condition" md-raised-button></button>
This may come late, but it is a viable and elegant method for applying a directive conditionally.
In the directive class create the input variable:
@Input('myDirective') options: any;
When applying the directive, set the apply property of the input variable:
<div [myDirective] = {apply: someCondition}></div>
In the method of the directive check for the variable this.options.apply and apply the directive logic based on the condition:
ngAfterViewInit(): void {
if (!this.options.apply) {
return;
}
// directive logic
}