I would like to have html in my database that contains component directives and then use that as part of a component template in Angular.
For an example, if I have:
<div [innerHTML]="injectMe"></div>
in App component template and this in the code for app component:
export class AppComponent {
injectMe = "<p>Paragraph</p> <app-injected></app-injected> ";
}
then create a component named injected:
template:
<p> injected works! </p>
code:
@Component({
selector: 'app-injected',
templateUrl: './injected.component.html',
styleUrls: ['./injected.component.css']
})
export class InjectedComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
I get a warning in the console that "sanitizing HTML stripped some content". Following that chain, I tried to add a call to mark the string as safe using: this.sanitizer.bypassSecurityTrustHtml
export class AppComponent {
injectHtml = "<p>Paragraph</p> <app-injected></app-injected> ";
injectMe: any;
constructor(private sanitizer: DomSanitizer){
this.injectMe = this.sanitizer.bypassSecurityTrustHtml(this.injectHtml);
}
}
That results in the directive tag being left in the HTML with no console warning about sanitize removing anything, but the component does not seem to be processed by Angular. I saw you can use component factory to create components dynamically, however, I don't think this will help in my case unless I want to manually parse the HTML to check for the components.