I'm having a problem in my app when using ngModel on the form with components of Material Design Lite. I was warned that I had to create a directive, as shown in the sample code.
import {Directive, AfterViewChecked, AfterViewInit} from '@angular/core';
declare var componentHandler: any;
@Directive({
selector: '[mdl]'
})
export class MDL implements AfterViewChecked, AfterViewInit{
ngAfterViewChecked() {
if (componentHandler) {
componentHandler.upgradeAllRegistered();
}
}
ngAfterViewInit(){
if (componentHandler) {
console.log('ngAfterViewInit ',componentHandler);
componentHandler.upgradeAllRegistered();
}
}
}
And use in the component template:
<div mdl class="mdl-layout mdl-js-layout mdl-layout--fixed-header">
...
</div>
Okay, this is working. But when I use [(ngModel)]
in my forms, I notice that the directive has no effect on input's. Just remove [(ngModel)]
that works again. If I use MDL checkbox for eg.
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect" for="remember">
<span class="mdl-switch__label">{{strings.label_remember}}</span>
<input name="remember" type="checkbox" id="remember" class="mdl-switch__input" [(ngModel)]="sess.persistent">
</label>
The initial value of the checkbox is not rendered to the user. even if I start the input with the checked
property. I do not know if i was clear.