【发布时间】:2018-11-09 06:23:54
【问题描述】:
我正在测试这个 html 表单:
<input #nhcInput type="text" class="form-control" name="nhc" id="field_nhc"
[(ngModel)]="paciente.nhc" maxlength="38" pattern="[0-9]+"/>
<div [hidden]="!(editForm.controls.nhc?.dirty && editForm.controls.nhc?.invalid)">
<small class="form-text text-danger"
[hidden]="!editForm.controls.nhc?.errors?.maxlength" jhiTranslate="entity.validation.maxlength" translateValues="{ max: 38 }">
This field cannot be longer than 38 characters.
</small>
TEH RESULT {{nhcInput.className}} //This line prints ng-valid/ dirty, touched correctly
我的组件中有这个:
paciente: Paciente = {nhc: '23423' } as Paciente;
it ('NHC cannot have more than 38 characters', async(() => {
comp.paciente.nhc = 'rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr' ;
console.log(fixture.nativeElement.querySelector('input[name="nhc"]').className);
fixture.detectChanges();
expect(fixture.nativeElement.querySelector('input[name="nhc"]').className.includes('ng-invalid')).toEqual(true);
}));
现在我想通过检查 vaidator 来测试有效性。 console.log 只打印出没有验证器类型的表单控件,因为它没有找到它。
我在我的组件中为此字段添加了一个验证器:
@ViewChild('editForm') editForm: any;
editform.controls["nhc"].setValidators([ Validators.maxLength(38)]);
但这不起作用。我在这里做错了吗?
谢谢!
【问题讨论】:
标签: javascript html angular