Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I initialize angular2 materialize component?

I just started using angular2 materialize and the CSS components work fine. However when I include a component that needs initialization (such as select) I have no idea how or where to do that.

This is a snippet of my form:

<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>

My component looks something like this:

import {Component, ElementRef, Inject, OnInit} from '@angular/core';
import {MaterializeDirective} from "angular2-materialize";

declare var jQuery:any;

@Component({
    selector: 'bsi-signup',
    styleUrls: ['assets/styles/app.component.css'],
    templateUrl: 'assets/templates/signup.component.html',
    directives: [MaterializeDirective],
    providers: []
})

export class SignupComponent implements OnInit {
    elementRef: ElementRef;

    constructor(@Inject(ElementRef) elementRef: ElementRef) {
        this.elementRef = elementRef;
}

    ngOnInit() {
        jQuery(this.elementRef.nativeElement).find('select:not([multiple])').material_select();
    }
}
like image 438
fowdie Avatar asked Mar 12 '23 18:03

fowdie


1 Answers

add the attribute materialize="material_select"

<select materialize="material_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>

The MaterializeDirective attribute directive (materialize) accepts any MaterializeCSS initialization call to apply to the element. The list of supported functions are provided by MaterializeCSS. Examples: collapsible, leanModal, tooltip, dropdown, tabs, material_select, sideNav, etc.

Source: https://www.npmjs.com/package/angular2-materialize

like image 149
muetzerich Avatar answered Mar 27 '23 07:03

muetzerich