【问题标题】:Mat-Select dropdown doesn't select any of optionsMat-Select 下拉菜单不选择任何选项
【发布时间】:2021-07-29 07:56:53
【问题描述】:

所以我用补丁值加载我的表单,由于某种原因,mat-select 没有根据值进行选择。

这是我所拥有的:

    constructor(private _formBuilder: FormBuilder,
        private productServices : ProductsService,
        private activatedRoute: ActivatedRoute,
        private router : Router) { 
          this.moods=[
              {mood: 'steak-0', viewValue: 'Steak'},
              {mood: 'pizza-1', viewValue: 'Pizza'},
              {mood: 'tacos-2', viewValue: 'Tacos'}
            
          ];
          }
        

      ngOnInit() {
        
            this.completeProfileForm = this._formBuilder.group({
              title :['',Validators.required],
              releaseDate: ['', Validators.required],
              resolution: ['', Validators.required],
              displayName: ['', Validators.required],
              description: ['', Validators.required],
              type: ['', Validators.required],
              primary_mood: ['', Validators.required],
              secondary_mood: ['', Validators.required],
              price: [null, Validators.required],
              
            });
      }
    <mat-form-field class="form-field" appearance="fill">
                    <mat-label>Primary Mood</mat-label>
                    <mat-select formControlName="primary_mood" >
                      <mat-option *ngFor="let mood of moods" [value]="mood.value">
                        {{mood.mood}}
                      </mat-option>
                    </mat-select>
                    <mat-error *ngIf="completeProfileForm.controls.primary_mood.touched && completeProfileForm.controls.primary_mood.invalid">
                        <span *ngIf="completeProfileForm.controls.primary_mood.errors.required">Please select your artwork Type.</span>
                    </mat-error>
                  </mat-form-field>

i tried to select any of the options the options not selected

你们能帮帮我吗

【问题讨论】:

  • 数据类型是{mood: 'steak-0', viewValue: 'Steak'},但您在 HTML 中使用了[value]="mood.value" 而不是mood.viewValue。是不是笔误?

标签: html node.js angular typescript angular-material


【解决方案1】:

我同意朱利安刘的观点。您的 HTML 中有错字。你说

[value]="mood.value"

但在您的对象中,该字段被命名为viewValue。

以这种方式更正您的 HTML:

<mat-option *ngFor="let mood of moods" [value]="mood.viewValue">
                    {{mood.mood}}
                  </mat-option>

要查看您的结果,请保留我的进一步建议并将这段代码放在ngOnInit() 的末尾。

this.completeProfileForm.get('primary_mood').valueChanges.subscribe(selection => console.log('you selected: ' + selection));

您现在应该在控制台中看到您的选择。现在你必须想办法利用它。

【讨论】:

  • 结果是:你选择:未定义
  • 对不起朋友,我没有看到你的错字。我根据它调整了我的答案。
猜你喜欢
  • 1970-01-01
  • 2017-04-12
  • 1970-01-01
  • 1970-01-01
  • 2014-11-28
  • 2023-03-31
  • 2018-04-01
  • 1970-01-01
相关资源
最近更新 更多