如何初始化angular2物化组件?

问题描述:

我刚开始使用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(); 
    } 
} 

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

喜每一个人!该工程是我:

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

是的,因为你需要其他角度文件,这只是组件中的一个配置示例,你可以发布你的代码来帮助你 –