The innerHtml is a property of HTML-Elements, which allows you to set it’s html-content programatically. There is also a innerText property which defines the content as plain text.
The [attributeName]="value" box bracket , surrounding the attribute defines an Angular input-binding. That means, that the value of the property (in your case innerHtml) is bound to the given expression, when the expression-result changes, the property value changes too.
So basically [innerHtml] allows you to bind and dynamically change the html-conent of the given HTML-Element.
import { Component, OnInit, ViewChild } from '@angular/core';import { AngularHtmlRecompileComponent, AngularHtmlRecompileService } from 'angular-html-recompile';
@Component({selector: 'app-root',templateUrl: './app.component.html',styleUrls: ['./app.component.scss']})export class AppComponent implements OnInit {
@ViewChild(AngularHtmlRecompileComponent, { static: true }) comp !: AngularHtmlRecompileComponent;
constructor(private angularHtmlRecompileService: AngularHtmlRecompileService) {}
public dataObject: any;
public template = `<div class="login-wrapper" fxLayout="row" fxLayoutAlign="center center"><mat-card class="box"><mat-card-header><mat-card-title>Register</mat-card-title></mat-card-header><form class="example-form"><mat-card-content><mat-form-field class="example-full-width"><input matInput placeholder="Username" [value]="Username" (keydown)="onControlEvent($event,'Username')"></mat-form-field><mat-form-field class="example-full-width"><input matInput placeholder="Email" [value]="Email" (keydown)="onControlEvent($event,'Email')"></mat-form-field><mat-form-field *ngIf="isShow" class="example-full-width"><input matInput placeholder="Password" [value]="Password" (keydown)="onControlEvent($event,'Password')"></mat-form-field><mat-form-field class="example-full-width"><mat-label>Choose a role...</mat-label><mat-select (selectionChange)="onControlEvent($event, 'selectedValue')"><mat-option [value]="roles" *ngFor="let roles of Roles">\{\{roles}}</mat-option></mat-select></mat-form-field></mat-card-content><button mat-stroked-button color="accent" class="btn-block" (click)="buttomClickEvent('submit')" >Register</button></form></mat-card></div>`;
ngOnInit(): void {
this.angularHtmlRecompileService.sharedData.subscribe((respose: any) => {if (respose) {switch (respose.key) {case `Username`:// Call any method on change of namebreak;case `Password`://Update password from main componentthis.comp[`cmpRef`].instance['Password'] = "Karthik";break;case `submit`://Get reference of all parameters on submit click//1. respose.data OR//use this.comp[`cmpRef`].instancebreak;default:break;}}});
this.prepareData();
}
prepareData() {
//Prepare data in following format only for easy binding//Template preparation and data preparation can be done once data received from service
// AngularHtmlRecompileComponent will not be rendered until you pass data
this.dataObject = [{ key: 'Username', value: 'Pranay' },{ key: 'Email', value: 'abc@test.com', },{ key: 'Password', value: 'test123', },{ key: 'Roles', value: ['Admin', 'Author', 'Reader'] },{ key: 'isShow', value: this.updateView() }];
}
updateView() {//Write down logic before rendering to UI to work ngIf directivereturn true;}}
添加模块到app.module.ts文件
import { NgModule } from '@angular/core';import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';import { AngularHtmlRecompileModule } from "angular-html-recompile";
@NgModule({declarations: [AppComponent],imports: [BrowserModule,AngularHtmlRecompileModule],providers: [],bootstrap: [AppComponent]})export class AppModule { }