Was trying to use Angular Sanitize API with the user input entered by the user inside angular form.
But the Angular's Sanitize() is not escaping the input entered.
.ts
export class AppComponent {
userName = 'Angular';
constructor(private domSanitizer: DomSanitizer) {}
onSave(): void {
console.log('save clicked api call');
this.sanitizeUserName();
}
sanitizeUserName(): string {
const domSanitize = this.domSanitizer.sanitize(1, this.userName);
console.log('sanitized text/html', domSanitize);
return domSanitize;
}
}
.html
<form #userForm="ngForm">
<label>Username: </label>
<input type="text" [(ngModel)]="userName" name="username" />
<div>
<span>View : </span>
<div [innerHTML]="userName"></div>
</div>
<br />
<br />
<button type="button" (click)="onSave()">Save</button>
</form>
Please find the live example here. In component.ts I have added few examples to verify if the input sanitization.
If someone can help me out.