英文:
Angular Material - passing value to radio group
问题
"mat-radio-group" 中传递值为什么不起作用?这两个按钮都没有被选中。但如果我在 "mat-radio-group" 中明确使用 "value="2"",一切都正常。
类内的 TypeScript:
radioValue = 2;
HTML:
<mat-radio-group [value]="radioValue">
<mat-radio-button class="mr-2" value="1">男</mat-radio-button>
<mat-radio-button value="2">女</mat-radio-button>
</mat-radio-group>
英文:
Why passing the value to mat-radio-group
doesn't work? Any of these two buttons is checked. But if I explicitly use value="2"
in mat-radio-group
everything works fine.
TS inside class
radioValue = 2;
HTML
<mat-radio-group [value]="radioValue">
<mat-radio-button class="mr-2" value="1">Male</mat-radio-button>
<mat-radio-button value="2">Female</mat-radio-button>
</mat-radio-group>
答案1
得分: 1
问题与数据类型有关,您正在合并string
和number
。请按照以下方式更改您的模板(还要在mat-radio-button
中使用[value]
):
<mat-radio-group [value]="radioValue">
<mat-radio-button class="mr-2" [value]="1">男</mat-radio-button>
<mat-radio-button [value]="2">女</mat-radio-button>
</mat-radio-group>
英文:
The problem is related to the data type, you're merging string
and number
. Change your template as follows (also use [value]
with mat-radio-button
):
<mat-radio-group [value]="radioValue">
<mat-radio-button class="mr-2" [value]="1">Male</mat-radio-button>
<mat-radio-button [value]="2">Female</mat-radio-button>
</mat-radio-group>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论