2016-07-06 84 views
3

我剛開始使用angular2物化和CSS組件工作正常。但是,當我包含需要初始化的組件(如select)時,我不知道如何或在哪裏做到這一點。如何初始化angular2物化組件?

這是我的表單的一個片段:

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

回答

6

添加屬性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> 

的MaterializeDirective屬性指令(物化)接受將任何MaterializeCSS初始化調用應用於該元素。 MaterializeCSS提供支持的功能列表。例如:可摺疊,leanModal,工具提示,下拉菜單,選項卡,material_select,sideNav等

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

+0

@ muetzerich好的,謝謝,我居然發現自己第二個我張貼的問題(典型值)。 :P – fowdie

0

喜每一個人!該工程是我:

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 
 
    } 
 

 

 

 
}

+0

當你運行你的代碼片段時,這會引發錯誤 –

+0

是的,因爲你需要其他角度文件,這只是組件中的一個配置示例,你可以發佈你的代碼來幫助你 –