【发布时间】:2021-03-10 15:20:16
【问题描述】:
我遇到了与制作所选垫子相关的树问题(请不要让我问 3 个问题,因为它们都是相关的)
- 在我单击空白区域之前,该控件不可见
- 无论我选择什么,所选选项都不会显示(不保留)
- 也许第一个问题可以通过选择默认选项来解决
忽略按钮下方的红色消息只是提醒
当我点击空白处时,会出现选择选项。
选择后控件处于无效模式
没有任何控制台/角度错误
这是组件
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';
@Component({
selector: 'app-game-round',
templateUrl: './game-round.component.html',
styleUrls: ['./game-round.component.css']
})
export class GameRoundComponent implements OnInit {
moves : string[] = [];
selectedMove: string = 'Rock';
currentRoundInfo = new FormGroup({
moves: new FormControl('', [Validators.required]),
});
constructor(
private router:Router) { }
ngOnInit(): void {
this.backService.getGameMoves()
.subscribe((data: string[]) => {
this.moves = data;
//this.currentRoundInfo.setValue({moves : data}) ;
console.log(`getGameMoves:${this.moves}`);
});
//trying to set a defult value
this.currentRoundInfo.setValue({
moves: 'Paper'
});
}
moveSelected(move: string) {
console.log("moveSelected:" + move) ;
this.selectedMove = move ;
}
onSubmit() {
console.log("next") ;
}
}
这里是 HTML
<div class="full-width-centered">
<form [formGroup]="currentRoundInfo" (ngSubmit)="onSubmit()">
<div class="full-width" >
<mat-form-field floatLabel="never" appearance="outline">
<mat-label>Select your move:</mat-label>
<mat-select [(value)]="selectedMove" formControlName="moves" placeholder="test placeholder" (selectionChange)="moveSelected($event.value)">
<mat-option *ngFor="let m of moves" [value]="m">
{{m}}
</mat-option>
</mat-select>
</mat-form-field>
</div>
<br>
<div>
<button type="submit">NEXT</button>
</div>
</form>
</div>
我发现这个问题是第一个问题,但没有提供答案。
mat-select doesn't display until i click on it
我也尝试了许多其他类似的答案,但都没有运气(将浮动标签更改为始终并设置占位符值)
【问题讨论】: