I have a directive that i have created in angular2 that changes the innerHTML
of a html element. Here is a simple version of the directive
import { Directive, ElementRef, Input, DoCheck } from '@angular/core';
@Directive({
selector: '[fieldName]'
})
export class FieldNameDirective implements DoCheck {
element: HTMLElement;
@Input('fieldName')
fieldId: number;
cached: number;
constructor(
el: ElementRef,
private moduleService: ModuleService) {
this.element = el.nativeElement;
}
ngDoCheck() {
if (this.cached != this.fieldId) {
// store cached
this.cached = this.fieldId;
this.element.innerHTML = 'TEST ME';
}
}
}
Now i want to change this directive so that it can contain a routerLink path, something like this
if (this.fieldId == 1)
this.element.innerHTML = 'NORMAL TEXT';
else
this.element.innerHTML = '<a routerLink="/path/to/go/to">TEXT WITH LINK</a>';
But doing this doesnt seem to actually generate a href link on the a tag.
In angular1, i think i would need to use the $compile
service and compile the HTML for it to work. DO i have to do something similar in angular2,a nd if so how?
I am using the new @angular/router
not the deprecated one.
Thanks in advance