角度 2 单选按钮验证



我正在尝试通过 temmplate 驱动但不起作用来验证角度 2 中的单选按钮。我在谷歌上搜索过,也没有人正确回答。任何天才都能回答这个问题?

https://stackblitz.com/edit/angular-zpyekb?file=src/app/app.component.html

app.component.html

 <form #f="ngForm"> 
<div *ngFor="let option of options">
<div class="radio">
<input type="radio"
           name="radio"
           id="radio-{{option.id}}"
           [(ngModel)]="value"
           [value]="option.value"/> 
    <label for="radio-{{option.id}}">{{option.id}}
    </label> 
 </div>
 </div>
 <div *ngIf="f.submitted">Please select</div>
 <button>Submit</button>
 </form>
<div *ngFor="let option of options">
        <div class="radio">
            <input type="radio"
               name="radio"
               id="radio-{{option.id}}"
               [(ngModel)]="value"
               [value]="option.value"
               required
               /> 
        <label for="radio-{{option.id}}">{{option.id}}
        </label> 
    </div>
  </div>
  <div *ngIf="f.submitted && !f.valid">Please select</div>

May be you are looking for the above validation. If not can you please help to know what exactly you are looking for ?

最新更新