POV
POV

Reputation: 12015

When to use NO_ERRORS_SCHEMA and CUSTOM_ELEMENTS_SCHEMA?

When we have to use NO_ERRORS_SCHEMA and CUSTOM_ELEMENTS_SCHEMA in Angular?

What do they mean? Is it config for CSS styles?

Upvotes: 15

Views: 8227

Answers (2)

Gauthier Peel
Gauthier Peel

Reputation: 1518

See https://angular.io/api/core/NO_ERRORS_SCHEMA => Defines a schema that allows any property on any element.

And https://angular.io/api/core/CUSTOM_ELEMENTS_SCHEMA => Defines a schema that allows an NgModule to contain the following: Non-Angular elements named with dash case (-). Element properties named with dash case (-). Dash case is the naming convention for custom elements.

So NO_ERRORS_SCHEMA is less restrictive, but anything unknown in the template is accepted

Upvotes: 1

Wandrille
Wandrille

Reputation: 6821

In your tests, when you define your TestBed, you need to import or declare all the dependencies of your component.

BUT sometimes, it can be really annoying and long to write it, when your tests aren't on this particular child components.

This is the case where you can use NO_ERRORS_SCHEMA. It will ignore all the errors saying that it doesn't know the child component <some-child>...</some-child>. But keep in mind that it's not a good practice to use NO_ERRORS_SCHEMA. As an alternative, you can create your own mock of component or use this library ng-mocks which is very helpful.

CUSTOM_ELEMENTS_SCHEMA is less permissive .

Upvotes: 9

Related Questions