Angular 2 – EXCEPTION:_this.subscribe不是函数 – 自定义验证

前端之家收集整理的这篇文章主要介绍了Angular 2 – EXCEPTION:_this.subscribe不是函数 – 自定义验证前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试实现自定义验证规则来验证是否选中了复选框.

但是我收到了一个错误

error_handler.js:46 EXCEPTION: _this.subscribe is not a function

当我尝试添加自定义验证器时

validator.ts

import {Control} from "angular/common";

 interface ValidationResult {
     [key: string]: any;
 }

 export class CustomValidators {

     static validateChecked(c: Control): ValidationResult {
         return (c.value);
     }
 }

Component.ts

import {Component} from '@angular/core';

import {FormBuilder,FormGroup,Validators,FormControl} from '@angular/forms';
import {CustomValidators} from './validators.ts'

@Component({
    selector: 'wizard',templateUrl: './template.html',})

/**
 * @todo - check how first select is automatically active
 * @todo - export form presets to form class
 */
export class Wizard {
    myForm: FormGroup;

    privacy: boolean;


    // Prefilling the FormBuilder
    constructor(private horizonService: HorizonService,fb: FormBuilder) {
        this.myForm = fb.group({
            'privacy': ['',Validators.required,CustomValidators.validateChecked],});
    }

    onSubmit(values: string): void {
        console.log('you submitted value: ',values);
    }
}
如果需要多个验证器,则应使用Validators.compose.所以你的代码应该是这样的:
constructor(private horizonService: HorizonService,fb: FormBuilder) {
    this.myForm = fb.group({
      'privacy': ['',Validators.compose([Validators.required,CustomValidators.validateChecked])],});
  }

您可以阅读有关验证器here的精彩文章.

原文链接:https://www.f2er.com/angularjs/141519.html

猜你在找的Angularjs相关文章