I have a input field which is not getting disabled after adding directive. If the directive is removed it works perfectly fine.
<div class="form-group row">
<label class="col-4 col-form-label">Date<span class="text-danger">*</span></label>
<div class="col-7">
<input type="text"
appDatePicker
[disabled]="currentOperation === 'view' || currentOperation ==='delete'"
required
value="{{holiday.off_date}}"
name="description"
[(ngModel)]="holiday.off_date"
class="form-control"
placeholder="01/01/2014">
</div>
</div>
Directive
import {Directive, ElementRef, forwardRef, OnInit} from '@angular/core';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
declare const $: any;
@Directive({
selector: '[appDatePicker]',
providers: [{
provide: NG_VALUE_ACCESSOR, useExisting:
forwardRef(() => DatePickerDirective),
multi: true
}]
})
export class DatePickerDirective implements OnInit, ControlValueAccessor {
value: string;
constructor(private el: ElementRef) {
}
ngOnInit() {
$(this.el.nativeElement).datepicker({
autoclose: true,
todayHighlight: true,
format: 'dd/mm/yyyy'
}).on('change', e => this._onChange(e.target.value));
}
private _onChange(_) {
}
private _onTouched(_) {
}
registerOnChange(fn: any): void {
this._onChange = fn;
}
registerOnTouched(fn: any): void {
this._onTouched = fn;
}
setDisabledState(isDisabled: boolean): void {
}
writeValue(val: string): void {
this.value = val;
}
}
I fear this problem is linked with the question I asked before. Link to that question is here
I have just tried disabled property, maybe other attributes will also not work.
Any help would be highly appreciated.
After searching i found out that ControlValueAccessor has one method which can be used to disable the state. The following can be achieved by adding this.
setDisabledState(isDisabled: boolean): void {
$(this.el.nativeElement).attr('disabled', isDisabled);
}