我在angular—project中使用react 式表单,我使用mat—country—select在子FormComponent中的select中显示国家.

<mat-select-country class="full-width"
                            [formControl]="form.controls.country"
                            [required]="true"
                            appearance="outline"
                            [itemsLoadSize]="0"
                            [value]="DEFAULT_COUNTRY"
                            label="Choose country">
          
        </mat-select-country>

我试图使模式对话窗口更新一个播放器在父Playerupet,有这样的 struct 如下所示.

export interface Player extends BaseResponse {
  lastName: string;
  firstName: string;
  dateOfBirth: Date;
  country: string;
  height: number;
  weight: number;
  position: string;
  jerseyNumber: number;
  teamName: string;
  teamId?: string | null;
  photoPath?: string | null;
}

在PlayerComponent中,在打开更新对话框窗口之前,我应该初始化表单并将表单传递给我FormComponent

export class PlayersComponent implements OnInit {
  countries: Country[] = COUNTRIES;
 playerActionsForm!: FormGroup<PlayerActionsForm>;

fillUpdatePlayerForm(player: Player) {
    this.playerActionsForm.controls.id.setValue(player.id);
    this.playerActionsForm.controls.firstName.setValue(player.firstName);
    this.playerActionsForm.controls.lastName.setValue(player.lastName);
    this.playerActionsForm.controls.birthDay.setValue(player.dateOfBirth);
    const country = this.countries.find(
      (country) => country.name === player.country,
    );
    this.playerActionsForm.controls.country.setValue(country ? country : null);
    this.playerActionsForm.controls.height.setValue(player.height);
    this.playerActionsForm.controls.weight.setValue(player.weight);
    this.selectTeams$
      .pipe(
        map((teams) => teams?.find((team) => team.id === player.teamId)),
        take(1),
        untilDestroyed(this),
      )
      .subscribe((team) => {
        this.playerActionsForm.controls.team.setValue(team ? team : null);
      });
    this.playerActionsForm.controls.jerseyNumber.setValue(player.jerseyNumber);
    this.playerActionsForm.controls.position.setValue(player.position);
    this.playerActionsForm.controls.experiences.setValue([]);
  }

  initializeActionsForm() {
    this.playerActionsForm = new FormGroup<PlayerActionsForm>({
      id: new FormControl(''),
      firstName: new FormControl('', [
        Validators.required,
        Validators.pattern(NAME_PATTERN),
      ]),
      lastName: new FormControl('', [
        Validators.required,
        Validators.pattern(NAME_PATTERN),
      ]),
      birthDay: new FormControl(null, [
        Validators.required,
        dateValidator(moment().subtract(18, 'years').toDate(), 'max'),
      ]),
      country: new FormControl(null, Validators.required),
      height: new FormControl(1.01, [
        Validators.required,
        Validators.min(1.01),
      ]),
      weight: new FormControl(50.01, [
        Validators.required,
        Validators.min(50.01),
      ]),
      team: new FormControl(null),
      position: new FormControl('', Validators.required),
      jerseyNumber: new FormControl(0, [
        Validators.required,
        Validators.min(0),
      ]),
      experiences: new FormArray<FormGroup<PlayerExperienceForm>>([]),
    });
  }

  ngOnInit(): void {
    this.initializeActionsForm();
    
  }

我try 使用viewChild来获取mat—country—select [Country]的Input属性,但它不起作用.现在我使用了我的预定义数组COUNTRIES,它是手工创建的.但我需要的解决方案,可以很容易地得到某个国家的名称,而不需要手动创建常量

推荐答案

您可以从国家导入国家

安装后material 角/国家

ng add @angular-material-extensions/select-country

您可以在组件中

import {Country,MatSelectCountryComponent,COUNTRIES_DB} 
                      from '@angular-material-extensions/select-country'; 

//or, e.g. if you want the countries in Spanish
import {Country,MatSelectCountryComponent,COUNTRIES_DB_ES} 
               from '@angular-material-extensions/select-country'; 

注意:如果你想在. html中使用,你需要在. ts中声明一个变量

COUNTRIES=COUNTRIES_DB

好吧,当我们想给FormControl添加一个不是显示的值时,有时我们可以使用ngModel的方法,

一个简单

<mat-select-country [ngModel]="form.get('country')?.value"
                    (ngModelChange)="form.get('country')?.setValue($event)"
                    [ngModelOptions]={standalone:true}
</mat-select-country>

与formControl相同.请注意ngModel是formControl的值,当更改时,我们生成一个setValue

我们要把整个对象赋给formControl.所以

<mat-select-country  [ngModel]="form.get('country')?.value?.name"
                    (ngModelChange)="setValue(form.get('country'),$event)"
                    [ngModelOptions]={standalone:true}
</mat-select-country>

setValue(control:AbstractControl,countryName:string)
{
  const country=COUNTRIES.find(x=>x.name==countryName)
  control.setValue(country)  
}

注意:您可以使用patchValuesetValue来"填充"formGroup(setValue需要所有属性,但patchValue不需要)

const country = this.COUNTRIES.find(
  (country) => country.name === player.country,
);
this.playerActionsForm.setValue({...player,country:country})

注2:如果只想将countryName与表单Control进行签名,

const country = this.COUNTRIES.find(
  (country) => country.name === player.country,
);
this.playerActionsFroms.setValue(player)
this.playerActionsForm.controls.country.setValue(country ? country.name : null);

Javascript相关问答推荐

我试图实现用户验证的reduxstore 和操作中出了什么问题?

Redux工具包查询(RTKQ)端点无效并重新验证多次触发

IMDB使用 puppeteer 加载更多按钮(nodejs)

在react js中使用react—router—dom中的Link组件,分配的右侧不能被 destruct ''

函数返回与输入对象具有相同键的对象

我可以使用使用node.js创建的本地主机来存储我网站上提交的所有数据吗?没有SQL或任何数据库.只有HTML语言

未定义引用错误:未定义&Quot;而不是&Quot;ReferenceError:在初始化&Quot;之前无法访问';a';

对路由DOM嵌套路由作出react

本地库中的chartjs-4.4.2和chartjs-plugin-注解

Web Crypto API解密失败,RSA-OAEP

当标题被点击时,如何使内容出现在另一个div上?

重新呈现-react -筛选数据过多

如何让SVG图标在被点击和访问后改变 colored颜色 ,并在被访问后取消点击时恢复到原来的 colored颜色 ?

如何在Reaction中设置缺省值, Select 下拉列表,动态追加剩余值?

如何处理不带参数的redux thunk payloadCreator回调函数?

在使用JavaScript以HTML格式显示Google Drive中的图像时遇到问题

如何用react组件替换dom元素?

在HTML中使用meta标记来指定定制元数据以用于使用JavaScript进行检索是不是一个坏主意?

与find()方法一起使用时,Mongoose中的$or运算符没有提供所有必需的数据

在不使用AJAX的情况下将JavaScript数组值传递给Laravel控制器?