【问题标题】:How do I initialize angular2 materialize component?如何初始化 angular2 物化组件?
【发布时间】:2016-07-06 14:13:51
【问题描述】:

我刚开始使用 angular2 materialize 并且 CSS 组件工作正常。但是,当我包含需要初始化的组件(例如选择)时,我不知道如何或在哪里执行此操作。

这是我的表单的 sn-p:

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

【问题讨论】:

    标签: typescript angular materialize


    【解决方案1】:

    添加属性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 属性指令 (materialize) 接受任何 MaterializeCSS 初始化调用以应用于元素。支持的函数列表由 MaterializeCSS 提供。示例:collapsible、leanModal、tooltip、dropdown、tabs、material_select、sideNav 等。

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

    【讨论】:

    • @muetzerich 是的,谢谢,我实际上在发布问题的第二秒就注意到了自己(典型)。 :P
    【解决方案2】:

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

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

    【讨论】:

    • 是的,因为你需要其他的角度文件,这只是你组件中的一个配置示例,你可以发布你的代码来帮助你
    • 是的,工作!顺便说一句,由于您的名字和“更新 DE 字段”而不是“the”,我注意到您的母语是西班牙语。 Jajaja Gracias!
    猜你喜欢
    • 1970-01-01
    • 2016-11-25
    • 1970-01-01
    • 2019-01-24
    • 2016-10-11
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    相关资源
    最近更新 更多