我剛開始使用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();
}
}
@ muetzerich好的,謝謝,我居然發現自己第二個我張貼的問題(典型值)。 :P – fowdie