1

Below is my code, dynamically binded html objects are not calling the respective component function. In Angular1.x we can easily achieve by $compile but in angular2+ i don't know how to achieve like below. Any help is much appreciated.

import { DomSanitizer } from '@angular/platform-browser'

@Pipe({ name: 'safeHtml'})
export class SafeHtmlPipe implements PipeTransform  {
  constructor(private sanitized: DomSanitizer) {}
  transform(value) {
    console.log(this.sanitized.bypassSecurityTrustHtml(value))
    return this.sanitized.bypassSecurityTrustHtml(value);
  }
}

@Component({
  selector: 'my-app',
  template: `
    <div [innerHtml]="html | safeHtml">
    </div>
  `,
})
export class App {
  name:string;
  html: safeHtml;
  constructor() {
    this.name = 'Angular2'
    this.html = "<input type="text" (ngModelChange)="navigateToCasual($event)" name="name"><input type="radio" name="name">
    <input type="button" (click)="navigateToCasual()" name="name">";
  }
navigateToCasual(){
    console.log("clicked")
  }
}
user1892203
  • 479
  • 4
  • 12
  • Angular only processes HTML of components and only when components are compiled. Later added HTML is ignored and your `(ngModelChange)="..."` and `(click)="..."` are meaningless. You can compile components at runtime. See also https://stackoverflow.com/questions/38888008/how-can-i-use-create-dynamic-template-to-compile-dynamic-component-with-angular – Günter Zöchbauer Sep 28 '17 at 12:41
  • Based on the backend sql server table values, i have to dynamically create HTML check boxes with calling functions and show to the client. How can i achieve this in angular ? – user1892203 Sep 28 '17 at 12:50
  • As shown in the link in my previous comment ;-) – Günter Zöchbauer Sep 28 '17 at 12:54
  • AOT is default for Angular 5 and JIT Compiler will be depreciated, how can i achieve such functionality ? – user1892203 Sep 28 '17 at 12:54
  • Then there is no Angular way. You can query the elements after they are added and add event handlers using TypeScript code. – Günter Zöchbauer Sep 28 '17 at 12:55
  • I couldn't find information about JIT compiler being deprecated. Can you please provide a link? – Günter Zöchbauer Sep 28 '17 at 12:59

0 Answers0