我已经创建了这个自定义验证器,以检查电子邮件是否确实以特定域结尾:
import { AbstractControl } from '@angular/forms';
import { stringify } from '@angular/compiler/src/util';
export function validateEmailDomain(control: AbstractControl)
{
if(!stringify(control.value).endsWith('test.com'))
{
return {response: true};
}
return null;
}
}
然后将其应用于反应式formGroup的特定控件:
'email': new FormControl('', [Validators.email, validateEmailDomain]),
此脚本运行良好。但是,我有一个全局变量服务,在其中添加了我在项目中使用的所有变量,因此当我将其出售给组织或公司时,我只需更改一些变量,例如徽标图像,图标,标题和电子邮件服务器的域,而不是转到每个组件并手动进行更改。
因此,我需要将全局变量服务传递给自定义验证器,并检查控制值是否匹配。
在对堆栈溢出进行几次搜索之后,我读到我应该将导出函数转换为一个类,所以我做了以下事情:
import { AbstractControl } from '@angular/forms';
import { stringify } from '@angular/compiler/src/util';
import { GlobalVarService } from 'src/app/global-var.service';
export class emailDomain{
constructor(private globalVar: GlobalVarService){}
static validateEmailDomain(control: AbstractControl) {
if (!stringify(control.value).endsWith(this.globalVar.emailDomain)) {
return { response: true };
}
return null;
}
}
并在控件处:
'email': new FormControl('', [Validators.email, emailDomain.bind(validateEmailDomain)]),
该行不通,因为在上一行出现未定义的validateEmailDomain
错误。
答案 0 :(得分:1)
相反:
if (folder.WellKnownFolderName == WellKnownFolderName.Calendar)
{
IAppointmentsRespository appointmentsContext = new AppointmentsRepository(new SubscriptionContext());
Appointment appointments = await Appointment.Bind(subscription.service, itemEvent.ItemId, BasePropertySet.FirstClassProperties);
Item item =appointments as Item;
}
使用此
emailDomain.bind(validateEmailDomain)
回答其他问题(错误:emailDomain.bind(emailDomain.validateEmailDomain)
):
您可以在静态方法内使用动态类字段(globalVar)。因此可以尝试将此值作为静态方法参数传递,例如(我从头开始写代码,所以进行测试)
Property 'globalVar' does not exist on type 'typeof emailDomain'
当然,您需要更改// I assume that you have this.globalVar here (so no in emailDomain constructor, but in place where you use that class), and also you have control: FormCotnrol object
'email': new FormControl('', [
Validators.email,
emailDomain.bind( () => emailDomain.validateEmailDomain(control , this.globalVar) )
])
才能使用此参数
答案 1 :(得分:1)
您可以使验证器更通用:
export function validateEmailDomain(globalVar) {
return function (control) {
if(!stringify(control.value)
.endsWith(globalVar.emailDomain))
{
return {response: true};
}
return null;
};
}
然后,当您使用它时,只需传递全局变量:
'email': new FormControl('',
[Validators.email, validateEmailDomain(this.globalVar)]),
答案 2 :(得分:1)
我本可以通过以下方式做到这一点。
声明一个变量
validateEmailDomain: any;
//add service in the same component
constructor(private globalVar: GlobalVarService){}
// then in ngOnInit initialize your var
ngOnInit(): void {
this.validateEmailDomain = (control: FormControl) => {
// you have the control here and you can call any exported method like validateEmailDomain(control: AbstractControl, globalVar: GlobalVarService)
if (!stringify(control.value).endsWith(this.globalVar.emailDomain)) {
return { response: true };
}
return null;
};
}
然后像使用它
'email': new FormControl('', [Validators.email, this.validateEmailDomain]),
让我知道我是否错过了任何事情
更新
export function validateEmailDomain(control: AbstractControl, globalVar: GlobalVarService) {
// your validations
}
然后使用这种方法
this.validateEmailDomain = (control: FormControl) => {
return validateEmailDomain(control, this.globalVar);
};