【问题标题】:select default for radio buttons inside a loop modelled with dynamic ngModel为使用动态 ngModel 建模的循环内的单选按钮选择默认值
【发布时间】:2017-10-31 11:19:01
【问题描述】:

我有一个包含输入的 for 循环,每个循环由五个单选值组成,其名称和模型是动态的。

我想将值 1 设置为默认值,我可以使用通常的 html "checked" 或 "checked = checked" 但它只能在没有 ngModel 的情况下工作,如果我使用的是 ngModel,我必须在组件类中对其进行初始化但是因为它是动态的,所以我无法先初始化它。

还有其他选择吗?还是我遗漏了什么??

<div class="scorecard-attribute" *ngFor="let attribute of opportunity.scorecard.scorecard_attributes">
   <input type="radio" name="{{attribute.name}}" [value]=1 [(ngModel)]="newRating[attribute.id]" checked>
   <input type="radio" name="{{attribute.name}}" [value]=2 [(ngModel)]="newRating[attribute.id]">
   <input type="radio" name="{{attribute.name}}" [value]=3 [(ngModel)]="newRating[attribute.id]">
   <input type="radio" name="{{attribute.name}}" [value]=4 [(ngModel)]="newRating[attribute.id]">
   <input type="radio" name="{{attribute.name}}" [value]=5 [(ngModel)]="newRating[attribute.id]">

【问题讨论】:

    标签: javascript html angular angular2-ngmodel


    【解决方案1】:

    您可以在您的 HTML 文件中尝试此代码,

    <div class="scorecard-attribute" *ngFor="let attribute of opportunity.scorecard.scorecard_attributes">
       <input type="radio" [value]="attribute" [(ngModel)]="newRating" (click)="click(attribute)">{{attribute.name}}
    </div>
    

    将此代码添加到组件文件中,

    newRating:any=opportunity.scorecard.scorecard_attributes[0];
    
    click(obj){
       console.log(obj);
    }
    

    谢谢,

    【讨论】:

      猜你喜欢
      • 2020-01-21
      • 2015-08-25
      • 2016-04-06
      • 2018-01-31
      • 2013-03-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-04
      • 1970-01-01
      相关资源
      最近更新 更多