Materialize: Initializing Select in Angular causes ERROR TypeError: $els.each is not a function

Created on 28 Jan 2018  路  3Comments  路  Source: Dogfalo/materialize

I have included Materialize 1.0 Alpha in my Angular 5 Application by calling this command:

npm install [email protected] --save

Then in .angular-cli.json

"scripts": [
        "../node_modules/materialize-css/dist/js/materialize.min.js"
      ],

And then in my Component:

import { Component, OnInit } from '@angular/core';
import * as M from 'materialize-css';
import {AfterViewInit} from "@angular/core/src/metadata/lifecycle_hooks";

@Component({
  selector: 'app-my',
  templateUrl: './my.component.html',
  styleUrls: ['./my.component.scss']
})
export class MyComponent implements AfterViewInit {

  constructor() { }

  ngAfterViewInit() {
    const elem = document.querySelector('select');

    M.Select.init(elem, '');
  }
}

in HTML:

  <div class="input-field col s12">
    <select>
      <option value="" disabled selected>Choose your option</option>
      <option value="1">Option 1</option>
      <option value="2">Option 2</option>
      <option value="3">Option 3</option>
    </select>
    <label>Materialize Select</label>
  </div>

And it causes this Exception:

ERROR TypeError: $els.each is not a function
at Function.init (materialize.js:11543)
at MyComponent.ngAfterViewInit (my.component.ts:17)

Most helpful comment

Really appreciate for your prompt answer.
How can I initialize the materialize-css in Angular4 in the way that I can execute this code:

    const elem = document.querySelector('select');

    M.Select.init(elem, '');

All 3 comments

Used this Command to install solved the Problem

npm install materialize-css@next --save

Your selector and initialization is probably wrong.

Really appreciate for your prompt answer.
How can I initialize the materialize-css in Angular4 in the way that I can execute this code:

    const elem = document.querySelector('select');

    M.Select.init(elem, '');
Was this page helpful?
0 / 5 - 0 ratings

Related issues

serkandurusoy picture serkandurusoy  路  3Comments

hartwork picture hartwork  路  3Comments

acierpinski picture acierpinski  路  3Comments

ruslandzhumaev picture ruslandzhumaev  路  3Comments

ReiiYuki picture ReiiYuki  路  3Comments