简体   繁体   English

Angular 5反应性表单-通过html更新formControl不会更新值,只有控件

[英]Angular 5 reactive form- updating formControl through the html does not update the value, only the control

I have a service with a FormGroup named mainForm- 我有一个名为mainForm-的FormGroup服务。

export class DepositFormDataService{

  this.mainForm = this.formBuilder.group({
            title: ['', Validators.required],
            category: '',
            type: '',
            languages: this.formBuilder.array([]),
        });
    }

    get title(): FormControl{
        return this.mainForm.get('title') as FormControl;
    }
}

In a component I use this service- 在组件中,我使用此服务-

 constructor(public depositFormDataService: DepositFormDataService) {}

HTML: HTML:

 <input matInput [formControl]="depositFormDataService.title">

When the user writes "b" for example I see in the debug a difference if I look on the FormGroup depositFormDataService.mainForm: 例如,当用户写“ b”时,如果在FormGroup depositFormDataService.mainForm上查找,我会在调试中看到不同:

in: Controls --> title --> value = b 在:控件->标题->值= b

in: value --> title --> null in:值->标题->空

Why? 为什么?

I found this post in git- https://github.com/angular/angular/issues/13792 我在git- https://github.com/angular/angular/issues/13792中找到了这篇文章

I tries to do this hack in the component- 我尝试在组件中进行此破解-

ngOnInit() {
    this.onChanges();
}

onChanges(): void {
    this.depositFormDataService.title.valueChanges
        .pipe(takeUntil(this.titleDestroy))
        .subscribe(value => {
            this.depositFormDataService.mainForm.get('title').patchValue(value, {emitEvent: false});
}

But it did not work and the value was still "null". 但是它没有用,并且值仍然是“ null”。

Any idea how to make the value be sync with the value inside the controls? 任何想法如何使值与控件内的值同步?

It causes a strange problem that depositFormDataService.mainForm.status differs from the depositFormDataService.title.status. 这会导致一个奇怪的问题,即depositFormDataService.mainForm.status与depositFormDataService.title.status不同。

我认为在HTML中,您应该使用formControlName而不是formcontrol,如下所示:

<input  formControlName="title" matInput >

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 angular 5,reactive form-重置所需的表单控件不会重置输入下的所需错误 - angular 5, reactive form- resetting a required form control does not reset the required error under the input 在Angular反应形式中读取复选框FormControl值 - Reading Checkbox FormControl value in Angular Reactive Form Angular 5-反应形式-是否可以使用变量修补值? - Angular 5 - reactive form- is there a way to patch value using a variable? 在 HTML 中以反应形式从 formArray 访问 formControl 的值 - Access value of formControl from formArray in reactive form in HTML 反应性 Forms 不更新表单字段中的值 Angular 7 - Reactive Forms Not Updating Value In Form Field Angular 7 Angular Reactive Form 单元测试:HTML 值和控制值不同步 - Angular Reactive Form Unit Testing: HTML value and control value out of sync 带HTML字符串的角反应形式设置值 - Angular reactive form setting value with HTML string 角度2/4 —将单选按钮[value]绑定到对象或函数时,为什么此反应式表单值不更新? - Angular 2/4 — When binding a radio button [value] to an object or function, why does this reactive form value not update? Angular - 测试响应式表单控件启用值更改 - Angular - test reactive form control enable on value changes 如何在角度反应形式控件中添加小数,其值从 1 和更大开始 - how to add decimal in angular reactive form control with value starting with 1 and greater
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM