【问题标题】:How to use an Injectable Service in a custom Validator如何在自定义验证器中使用可注入服务
【发布时间】:2019-01-17 06:09:39
【问题描述】:

我有这个场景:-

我正在我的应用中创建一个自定义验证器,它使用服务来获取数据

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)

无论如何我可以在该函数中使用角度服务吗??

【问题讨论】:

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


【解决方案1】:

替换这个

let iss : IndianStatesAndCitiesService;

var iss = new IndianStatesAndCitiesService();

你还没有创建服务对象试试这个

【讨论】:

    猜你喜欢
    • 2017-07-27
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-01
    • 2018-01-07
    相关资源
    最近更新 更多