I just started using angular2 materialize and the CSS components work fine. However when I include a component that needs initialization (such as select) I have no idea how or where to do that.
This is a snippet of my form:
<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>
My component looks something like this:
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();
}
}
add the attribute 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.
Source: https://www.npmjs.com/package/angular2-materialize
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With