Angular 需要自定义组件
Angular custom component required
有没有一种方法可以在我的自定义组件上使用 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;
}
}
如果 _date
(或 date
)为空或无效日期(因为 min/max),我希望我的表单在其中使用此组件禁用提交按钮.
有什么办法可以为我的组件实现这样的功能吗?
感谢您的帮助!
让它实现 Validator
接口:
export class DatePickerComponent implements ControlValueAccessor, Validator {...}
它只需要一个功能,validate
:
public validate(c: FormControl) {
// Business logic. If no error, return null, if not :
return { required: true };
}
有没有一种方法可以在我的自定义组件上使用 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;
}
}
如果 _date
(或 date
)为空或无效日期(因为 min/max),我希望我的表单在其中使用此组件禁用提交按钮.
有什么办法可以为我的组件实现这样的功能吗?
感谢您的帮助!
让它实现 Validator
接口:
export class DatePickerComponent implements ControlValueAccessor, Validator {...}
它只需要一个功能,validate
:
public validate(c: FormControl) {
// Business logic. If no error, return null, if not :
return { required: true };
}