Angular 8: Passing html as input variable
You can place your html in a string like
htmlStr = "<strong>This is an example</strong>";
and pass it through to a service:
this.whateverService.setHtml(this.htmlStr);
then in the receiving component:
import { WhateverService } from 'src/app/shared/service/whatever.service';
export class ReceivingComponentThing implements OnInit {
htmlExample = '';
constructor(private whateverService: WhateverService) {}
}
ngOnInit() {
// have a getter/setter in service however you like
this.htmlExample = this.whateverService.getHtmlExample();
}
in your template:
<div [innerHtml]="htmlExample"><div>
We can use innerHTML to achieve this
Sample example demonstrating this,
parent.component.ts,
export class ParentComponent {
htmlOneAsString = `
<div>Welcome Text Header</div>
`;
htmlTwoAsString = `
<div>Welcome Text Content</div>
`;
htmlAsString = `
<div><div>${this.htmlOneAsString}</div><div>${this.htmlTwoAsString}</div></div>
`;
}
parent.component.html,
<child [innerHTML]="htmlAsString"></child>
child.component.ts,
@Component({
selector: 'child'
})
export class ChildComponent {
@Input() htmlAsString: string;
}
child.component.html,
<div>{{htmlAsString}}</div>
I have found the solution, this can be done by:
<div class='wrapper'>
<div class="exclusive-css-defined-to-this-component">
<div><ng-content select="[content1]"></ng-content></div>
</div>
<div class="exclusive-css-defined-to-this-component-2">
<div><ng-content select="[content2]"></ng-content></div>
</div>
</div>
And we can use the component like:
<wrapper>
<div content>Any thing you want to put in content1</div>
<div content2>Any thing you want to put in content2</div>
</wrapper>