Force a variable to be a valid HTML template

91 Views Asked by At

In an Angular app, I am looking for a way to force a variable in TypeScript to be a valid HTML template, so that this will be compiled successfully:

let v = '<div>bla…</div>'

but this:

let v = '<cc></div>'

will cause a compilation error.

Is there such a possibility?

Edited:

If there is no way at compile time, maybe there is a way to make the Angular Cli Builder crash at build time in this case?

1

There are 1 best solutions below

0
Ivan Ivanyuk On

Not sure if it is possible to validate html strings at compile time.

Here is how to check validity of html at runtime Check if HTML snippet is valid with JavaScript

Depending on your use case, you may also need to sanitize html or bypass sanitizer, using https://angular.io/api/platform-browser/DomSanitizer