如何初始化 angular2 物化组件?

How do I initialize angular2 materialize component?

我刚开始使用 angular2 materialize,CSS 组件工作正常。但是,当我包含一个需要初始化的组件时(例如 select),我不知道如何或在哪里进行初始化。

这是我的表格的一个片段:

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

我的组件看起来像这样:

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

添加属性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.

来源:https://www.npmjs.com/package/angular2-materialize

大家好!!!这对我有用:

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
  }



}