TL; DR; You need to call the componentHandler.upgradeElement component after the elements have been entered into the DOM. The approach I used in the past is described in the example below.
EDIT If you want a declarative solution, this approach would look pretty good here, but I didn't use it myself.
I created a service that wraps a Material LiteHandler component
import { Injectable } from '@angular/core'; export interface ComponentHandler { upgradeDom(); } declare var componentHandler: ComponentHandler; @Injectable() export class MaterialService { handler: ComponentHandler; constructor() { this.handler = componentHandler; }
Then you call the service rendering function after the component has injected these elements into the DOM. In your case, this happens after *ngFor
This is a very contrived example, but it demonstrates "where" to invoke rendering.
import { Component, OnInit } from '@angular/core'; import { DataService } from 'services/data.service'; import { MaterialService } from 'services/material.service'; @Component({ selector: 'app-thing', templateUrl: ` <ul> <li *ngFor="let item of data"> {{data}} </li> </ul> ` }) export class ThingComponent implements OnInit { data: string[] constructor( private service: DataService, private material: MaterialService ) { } ngOnInit() { this.service.getData() .subscribe(data => { this.data = data; this.material.render(); }); } }
source share