ReV*_*0n_ 5 custom-component required ionic-framework ionic3 angular
有没有办法可以required在我的自定义组件上使用该属性?我的组件如下所示:
import { Component, Input, forwardRef } from '@angular/core';
import { Platform } from 'ionic-angular';
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';
export const CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => DatePickerComponent),
multi: true
};
@Component({
selector: 'date-picker',
templateUrl: 'date-picker.html',
providers: [CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR]
})
export class DatePickerComponent implements ControlValueAccessor {
@Input("label") label: string;
@Input("pickerFormat") pickerFormat: string;
@Input("displayFormat") displayFormat: string;
@Input("max") max: string;
@Input("min") min: string;
@Input("cancelText") cancel: string;
@Input("doneText") done: string;
@Input("valid") valid: boolean;
@Input("disabled") disabled: boolean;
private _mobile: boolean = false;
private _date: string;
constructor(private plt: Platform) {
if (plt.is("mobile"))
this._mobile = true;
}
get date(): string {
return this._date;
}
set date(value: string) {
this._date = value;
this.onChange(this._date);
}
public onChange(value: any): void { }
public onTouched(): void { }
public writeValue(obj: any): void {
if (this.date !== obj) {
this.date = obj;
}
}
public registerOnChange(fn: any): void {
this.onChange = fn;
}
public registerOnTouched(fn: any): void {
this.onTouched = fn;
}
}
Run Code Online (Sandbox Code Playgroud)
如果_date(或date)为空或不是有效日期(因为最小/最大),我希望我使用此组件的表单禁用提交按钮。有什么办法可以为我的组件实现这样的功能吗?
谢谢你的帮助!
小智 3
让它实现Validator接口:
export class DatePickerComponent implements ControlValueAccessor, Validator {...}
Run Code Online (Sandbox Code Playgroud)
它只需要一个函数validate:
export class DatePickerComponent implements ControlValueAccessor, Validator {...}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1165 次 |
| 最近记录: |