a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule. schemas' of this component to suppress this message. angular is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.
export CUSTOM_ELEMENTS_SCHEMA Defines a schema that will allow: any non-Angular elements with a - in their name, any properties on elements with a - in their name which is the common rule for custom elements.
This schema allows you to ignore the errors related to any unknown elements or properties in a template. The usage of this schema is generally discouraged because it prevents useful validation and may hide real errors in your template.
Just wanted to add a little bit more on this.
With the new angular 2.0.0 final release (sept 14, 2016), if you use custom html tags then it will report that Template parse errors
. A custom tag is a tag you use in your HTML that's not one of these tags.
It looks like the line schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
need to be added to each component where you are using custom HTML tags.
EDIT: The schemas
declaration needs to be in a @NgModule
decorator. The example below shows a custom module with a custom component CustomComponent
which allows any html tag in the html template for that one component.
custom.module.ts
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CustomComponent } from './custom.component';
@NgModule({
declarations: [ CustomComponent ],
exports: [ CustomComponent ],
imports: [ CommonModule ],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
export class CustomModule {}
custom.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'my-custom-component',
templateUrl: 'custom.component.html'
})
export class CustomComponent implements OnInit {
constructor () {}
ngOnInit () {}
}
custom.component.html
In here you can use any HTML tag you want.
<div class="container">
<boogey-man></boogey-man>
<my-minion class="one-eyed">
<job class="plumber"></job>
</my-minion>
</div>
This is fixed by:
a) adding schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
to every component or
b) adding
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
and
schemas: [
CUSTOM_ELEMENTS_SCHEMA
],
to your module.
This can also come up when running unit tests if you are testing a component with custom elements. In that case custom_elements_schema needs to be added to the testingModule that gets setup at the beginning of the .spec.ts file for that component. Here is an example of how the header.component.spec.ts setup would begin:
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
describe('HeaderComponent', () => {
let component: HeaderComponent;
let fixture: ComponentFixture<HeaderComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [PrizeAddComponent],
schemas: [
CUSTOM_ELEMENTS_SCHEMA
],
})
.compileComponents();
}));
Add the following under @NgModule({})
in 'app.module.ts' :
import {CUSTOM_ELEMENTS_SCHEMA} from `@angular/core`;
and then
schemas: [
CUSTOM_ELEMENTS_SCHEMA
]
Your 'app.module.ts' should look like this:
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
@NgModule({
declarations: [],
imports: [],
schemas: [ CUSTOM_ELEMENTS_SCHEMA],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
It didn't work for me either. I changed
CUSTOM_ELEMENTS_SCHEMA
for
NO_ERRORS_SCHEMA
which was suggested in this article: https://scotch.io/tutorials/angular-2-transclusion-using-ng-content
Now it works.
util.component.ts
@Component({
selector: 'app-logout',
template: `<button class="btn btn-primary"(click)="logout()">Logout</button>`
})
export class LogoutComponent{}
util.module.ts
@NgModule({
imports: [...],
exports: [
LogoutComponent
],
declarations: [LogoutComponent]
})
export class AccountModule{};
LogoutComponent Needs to be exported
dashboard.module.ts
import AccountModule
in module where we want to use <app-logout>
import { AccountModule } from 'util.module';
@NgModule({
imports: [
CommonModule, AccountModule
],
declarations: [DashboardComponent]
})
export class DashboardModule { }
dashboard.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-dashboard',
template: `<div><app-logout></app-logout></div>`
})
export class DashboardComponent implements OnInit {
constructor() {}
ngOnInit() {}
}
I am not required to import and use CUSTOM_ELEMENTS_SCHEMA
.
however its not working when dashboard.module is lazy loaded.
When using CUSTOM_ELEMENTS_SCHEMA
in case of lazy loading, error is suppressed but the component is not added to dom.
With components containing Angular Material, a similar error came up with my unit tests. As per @Dan Stirling-Talbert's answer above, added this to my component .spec file and the error was cleared from my unit tests.
Import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'
Then add the schema in the generated beforeEach() statement:
beforeEach(asyn(() => {
declarations: [ AboutComponent ],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
.compileComponents();
}));
My Karma error was: If 'mat-panel-title' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With