【问题标题】:How do I check a radio button dynamically in Angular?如何在 Angular 中动态检查单选按钮?
【发布时间】:2022-02-09 22:15:41
【问题描述】:

如果 type.location=='none',我正在尝试检查单选按钮,但这似乎不起作用。

<ng-container *ngFor="let type of topping.type">
    <input 
        type="radio" class="mr-1" name="{{topping.name}}" 
       id="{{type.location}}" [(ngModel)]="topping.location" 
        [value]="type.location"
        checked="type.location=='none'">
</ng-container>

【问题讨论】:

  • 用方括号括起来[checked]
  • 那行不通。
  • 你不能同时使用ngModelchecked
  • 只有topping.location='none',是的,不使用[已选中]

标签: html angular


【解决方案1】:

代码按原样工作,我忘记在数据库中将 type.location 字段设置为“none”,因此未在 html 页面中检查收音机。

【讨论】:

    【解决方案2】:

    试试

    [checked]=" type.location==='none' ? true : false" 
    ||
    [checked]="type.location=='none'"
    

    而不是

    checked="type.location=='none'" 
    

    【讨论】:

      【解决方案3】:
      <table *ngIf="optionList">  
          <tr *ngFor="let op of optionList; let i = index">
            <td>
                <input type="radio" name="optradio" [checked]=" (CONDITION HERE)"> 
                <label>{{op.option_text}} </label> 
            <td>
          </tr>
      </table>
      

      【讨论】:

        猜你喜欢
        • 2020-12-26
        • 2019-04-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-15
        • 1970-01-01
        • 1970-01-01
        • 2011-01-25
        相关资源
        最近更新 更多