3

I have included Angular Material in my angular element project. I have included all the references how the documentation suggests, but when I build seems like angular the material theme isn't included at all.

This is the link for watching what I mean: https://next.plnkr.co/edit/3JIbOfnyKiefS31N?open=lib%2Fscript.js

Material module for wrapping all the components

import {NgModule} from '@angular/core';
import {MatIconModule} from '@angular/material/icon';
import {MatInputModule} from '@angular/material/input';
import {MatSelectModule} from '@angular/material/select';
import {MatFormFieldModule} from '@angular/material';

@NgModule({
  exports: [
    MatSelectModule,
    MatIconModule,
    MatInputModule,
    MatFormFieldModule,
  ]
})
export class MaterialModule {}

AppModule

import { BrowserModule } from '@angular/platform-browser';
import {Injector, NgModule} from '@angular/core';

import {GalleryComponent} from './gallery/gallery.component';
import {createCustomElement} from '@angular/elements';

import {ImageService} from './gallery/image.service';
import { ImageComponent } from './gallery/image/image.component';
import { ImageDetailsComponent } from './gallery/image-details/image-details.component';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {MaterialModule} from './material.module';

@NgModule({
  declarations: [
    GalleryComponent,
    ImageComponent,
    ImageDetailsComponent
  ],
  imports: [
    MaterialModule,
    BrowserAnimationsModule,
    BrowserModule
  ],
  entryComponents: [GalleryComponent],
  providers: [ImageService],
})
export class AppModule {
  constructor(private injector: Injector) {}

  ngDoBootstrap() {
    const custom = createCustomElement(GalleryComponent, { injector: this.injector });
    customElements.define('slim-gallery', custom);
  }
}

The Style

@import "~@angular/material/prebuilt-themes/deeppurple-amber.css";

body {
  font-family: Roboto, Arial, sans-serif;
  margin: 0;
}

.basic-container {
  padding: 30px;
}

.version-info {
  font-size: 8pt;
  float: right;
}

Component template html

<mat-form-field>
  <mat-label>Favorite food</mat-label>
  <mat-select>
    <mat-option *ngFor="let food of foods" [value]="food.value">
      {{food.viewValue}}
    </mat-option>
  </mat-select>
</mat-form-field>
Bonfry
  • 163
  • 2
  • 12

3 Answers3

4

I was facing the same issue. Try adding this line (this is for indigo theme) on you app.component.css

@import "~@angular/material/prebuilt-themes/indigo-pink.css";

And then on your component ts use encapsulation none, like follows:

@Component({
 ...,
 encapsulation: ViewEncapsulation.None
})
0

In case someone is interested in the solution. My problmen was caused by ngZone: 'noop' in the main.ts file. It disables change detection.

So changing this

platformBrowserDynamic() .bootstrapModule(AppModule, { ngZone: 'noop' }) .catch(err => console.error(err)); to this

platformBrowserDynamic() .bootstrapModule(AppModule) .catch((err) => console.error(err)); solved my problem with Angular Material Elements.

-1

You have imported this:

import {MatFormFieldModule} from '@angular/material';

But the Material API documentation specifies the import like this:

import {MatFormFieldModule} from '@angular/material/form-field';

On stackBlitz: https://stackblitz.com/edit/angular-3l12yn

Robin De Schepper
  • 4,942
  • 4
  • 35
  • 56
Beller
  • 828
  • 1
  • 6
  • 19
  • If I remove that module, the result is the same. The problem could be Angular Elements but I don't know how resolve – Bonfry Sep 16 '19 at 17:01
  • Would you upload to stackblitz? (plunker doesn't says anything) I have copy&paste your code to stackBlitz and after that change the import path and works. Or remove this part : Favorite food and add placeholder ` {{food.viewValue}} ` – Beller Sep 16 '19 at 17:08
  • console says: ERROR Error: Failed to execute 'define' on 'CustomElementRegistry': the name "slim-gallery" has already been used with this registry – Beller Sep 16 '19 at 18:04
  • index.html contains: if you check this: https://stackblitz.com/edit/angular-3l12yn you can see loading. Which refers to **app.component.ts:** `@Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: [ './app.component.css' ] })` – Beller Sep 16 '19 at 18:15
  • I think I can't replicate on stackbliz because Angular create 2 different build(es5 and es2015), so the slim-gallery component is declared twice – Bonfry Sep 17 '19 at 05:19
  • **1.** I had made a components folder. **2.** I put there "gallery" related files. **3.** I added routing, gallery will be the default path `{path: '', component: GalleryComponent}`. **4.** added gallery.component and routing to app.module.ts. **5.** app.component.ts got `import { Router } from '@angular/router';` and `constructor(private router: Router){}`. **6.** app.component.html got ``. **7.** remove this part `encapsulation: ViewEncapsulation.ShadowDom,` from gallery.component.ts – Beller Sep 17 '19 at 06:33