如何在自定义验证程序中使用可注射服务

时间:2019-01-17 06:09:39

标签: angular function angular-services angular-validation angular-validator

我有这种情况:-

我正在使用应用程序来获取数据的应用程序中创建一个customvalidator

import { AbstractControl } from '@angular/forms';
import { IndianStatesAndCitiesService } from './indianstatesandcities.service';
export function SpecificState(control: AbstractControl) {
    let iss : IndianStatesAndCitiesService;
    let getiss : string[] = [];
    for(let i in iss.getStatesAndCities()){
        getiss.push(i);
    }
    if (control.value !== undefined && (getiss.includes(control.value))) {
        return { 'specificState': true };
    }
    return null;
}

但是它抛出以下错误:-

ERROR TypeError: Cannot read property 'getStatesAndCities' of undefined
    at SpecificState (specificstate.validator.ts:6)

反正我可以在该函数内使用角度服务吗?

1 个答案:

答案 0 :(得分:0)

替换此

let iss : IndianStatesAndCitiesService;

var iss = new IndianStatesAndCitiesService();

您尚未创建服务对象,请尝试