4

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();
    }
}

2 Answers 2

6

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

Sign up to request clarification or add additional context in comments.

1 Comment

@ muetzerich Yeah thanks, I actually noticed myself the second I posted the question (typical). :P
1

Hi every one !!! this works form me:

import { Component, OnInit } from '@angular/core';

declare var Materialize:any; //we declarate the var Materialize for use 

@Component({
  //your code
})
export class MyComponentComponent implements OnInit {

  constructor( ) {
    //your code
  }

  ngOnInit() {
    // your code
  }

  ngAfterViewChecked(){ // Respond after Angular initializes the component's views and child views.
    Materialize.updateTextFields();// update de fields when the document and the views a are ready
                                   // in case that the inputs are empty 
  }

  updateInformation(){
     // your code ...
     Materialize.updateTextFields(); // update the fields, 
                                     // is not necesary add the class="active" in the views
  }



}

2 Comments

Yes because you need other angular files , this is only a configuration example in your component, you can post your code for help you
Yup worked! Btw I noticed that your native language is Spanish due to your name and the "update DE fields" instead of "the". Jajaja Gracias!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.