简体   繁体   English

反应形式 patchValue() 与 ngValue 不起作用

[英]Reactive forms patchValue() with ngValue not working

I have one dropdown in reactive forms I have used the whole object as the value( {NAME:'test',CODE:'002'} ) for further processing, its working fine I am able to get selected item in .ts file.In edit mode, I have to bind selected item to drown using patchValue but it's not working.我有一个反应形式的下拉列表,我使用整个对象作为值( {NAME:'test',CODE:'002'} )进行进一步处理,它工作正常,我能够在 .ts 文件中获取所选项目。在编辑模式下,我必须使用 patchValue 将所选项目绑定到淹没,但它不起作用。

html html

                     <select class="form-group form-control" formControlName="county">
                                <option value="">-Select-</option>
                                <option *ngFor="let item of countyDataset" [ngValue]='item'>{{item.NAME}}
                                </option>
                            </select>

.ts code .ts 代码

bindDataValue(){
    const rec=county{
         CODE: "028"
          NAME: "LOS ALAMOS"
       }
this.form.patchValue(rec);
}
  <h1>Reactive Forms</h1>
    
  <div style="padding: 0 0 20px 20px">
  
    <h3>Value string</h3>
    <form [formGroup]="selectForm">
    <mat-form-field>
      <mat-select name="countryReactiveString" formControlName="selectedCountryControl" placeholder="Country">
        <mat-option *ngFor="let country of countries" [value]="country">{{country?.full}}</mat-option>
      </mat-select>
    </mat-form-field>
    </form>
    {{selectForm.value | json}}
  </div>


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


@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.scss' ]
})
export class AppComponent implements OnInit {
  selectForm: FormGroup;
  constructor(private fb: FormBuilder) {}
  
  countries: any = [
    {
      full: "Great Britain",
      short: "GB"
    },
    {
      full: "United States",
      short: "US"
    },
    {
      full: "Canada",
      short: "CA"
    }
  ];
  ngOnInit() {
    this.selectForm = this.fb.group({
      selectedCountryControl: []
    })
    this.selectForm.patchValue({
       selectedCountryControl: this.countries[0]
    })
  }

}

Here is demo it should workhttps://stackblitz.com/edit/angular-material-select-demo-d7edt6这是它应该工作的演示https://stackblitz.com/edit/angular-material-select-demo-d7edt6

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

相关问题 Angular 反应式 Forms,patchValue 不起作用,嵌套 forms - Angular Reactive Forms, patchValue not working, nested forms For 循环中的反应形式 patchValue() - Reactive Forms patchValue() in For loop Typescript:反应性 forms patchValue 不适用于私有属性和 getter/setter - Typescript: Reactive forms patchValue not working with private properties and getters/setters Angular 反应形式 patchValue 或 setValue 不起作用 - Angular 10 - Angular reactive forms patchValue or setValue not working - Angular 10 在具有父子路由的Reactive Forms中使用patchValue - Use Of patchValue in Reactive Forms with parent child routing angular 补丁值 json 具有反应性的数据 forms - angular patchValue json data with reactive forms Angular 5 反应形式 - 带有 FormArray 的 patchValue 不起作用 - Angular 5 reactive form - patchValue with FormArray not working 如何在测试角度2反应形式中使用setValue和patchValue - How to use setValue and patchValue in testing angular 2 reactive forms Angular 反应式 forms patchValue 对 ngbDatepicker 输入不起作用 - Angular reactive forms patchValue does not work on ngbDatepicker input Angular-Reactive Forms:如何在 patchValue 之后验证字段 - Angular- Reactive Forms: How to validate fields after patchValue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM