gpt4 book ai didi

angular - 输入时 FormControl 不更新

转载 作者:行者123 更新时间:2023-12-05 01:43:17 24 4
gpt4 key购买 nike

我有一个使用自定义组件作为输入的表单,它接收一个 ControlName。当我在组件中键入内容时,它不会更新“错误”属性。

app-pf-input-text.ts:

import { Component, OnInit, Input } from '@angular/core';

@Component({
selector: 'app-pf-input-text',
templateUrl: './pf-input-text.component.html',
styleUrls: ['./pf-input-text.component.scss']
})
export class PfInputTextComponent implements OnInit {

@Input() id: string;
@Input() name: string;
@Input() value: string;
@Input() placeholder: string;

//falta trim

@Input() maxlength: string;
@Input() minlength: string;

@Input() disabled: boolean;
@Input() required: boolean;

constructor() { }

ngOnInit() {
}
}

app-pf-input-text.html:

<div class="input-group">
<input
type="text" class="form-control">
</div>

我的表单:

import { Component, OnInit, Input } from '@angular/core';

import { FormArray, FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';

import { Empresa } from '../empresa.model';

@Component({
selector: 'app-empresa-cadastro',
templateUrl: './empresa-cadastro.component.html',
styleUrls: ['./empresa-cadastro.component.css']
})
export class EmpresaCadastroComponent implements OnInit {

empresaForm: FormGroup;

nomeControl: FormControl;

constructor(private fb: FormBuilder) {}

createForm() {
this.nomeControl = new FormControl('', [
Validators.required,
Validators.minLength(3),
Validators.maxLength(10)
]);
}

createFormControl() {
console.log('Criando FormControl');
}

onSubmit() {
const empresa = this.empresaForm.value as Empresa;
console.log('Empresa: ', empresa);
}

limpar() {
this.empresaForm.reset();
}

ngOnInit() {
this.createForm();
}

}

我的表单 html:

<form [formGroup]="empresaForm">
<div class="form-row">


<div class="col-md-4 mb-3">
<label for="validationServer01">Nome empresa</label>
<app-pf-input-text controlName="nome"></app-pf-input-text>
{{nomeControl.errors | json}} - {{nomeControl.dirty}}
<div class="form-control-feedback" *ngIf="nomeControl.errors && (nomeControl.dirty || nomeControl.touched)">
<div *ngIf="nomeControl.errors.required" class="has-error">
Campo obrigatorio
</div>
<div *ngIf="nomeControl.errors.maxlength">
Campo ultrapassou o tamanho maximo.
</div>
<div *ngIf="nomeControl.errors.minlength">
Campo não antingiu o tamanho minimo
</div>
</div>
</div>


</div>
</form>

它在“ControlName”中提供了一个初始值,但在我输入时它永远不会更新。

{{nomeControl.errors | json}} - {{nomeControl.dirty}}

返回:

{ “必需”:真 } - 假

当您在输入中输入内容时,这些值不会更新。

最佳答案

您正在混合响应式(Reactive)和模板驱动的形式。你的PfInputTextComponent有一些问题。使用 Reactive Forms,您不需要 required , maxLength , minLengthdisabled输入。你确实需要 FormControl输入。如果您使用的是 Reactive Forms,则可以在组件类中设置值和验证器,而不是在模板中。

this.nomeControl = new FormControl('', [
Validators.required,
Validators.minLength(3),
Validators.maxLength(10)
]);

这足以设置验证和初始值。您现在需要做的就是将此控件绑定(bind)到您的 html <input />使用 [formControl]指令。

app-pf-input-text.html

<div class="input-group">
<input [formControl]="ctrl" type="text" class="form-control">
</div>

app-pf-input-text.ts:

import { Component, OnInit, Input } from '@angular/core';
import { FormControl } from '@angular/forms';

@Component({
selector: 'app-pf-input-text',
templateUrl: './pf-input-text.component.html',
styleUrls: ['./pf-input-text.component.scss']
})
export class PfInputTextComponent implements OnInit {

@Input() ctrl: FormControl;
@Input() placeholder: string;

constructor() {}

ngOnInit() {}
}

form.component.html

<app-pf-input-text [ctrl]="nomeControl"></app-pf-input-text>

关于angular - 输入时 FormControl 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49784580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com