简体   繁体   中英

Angular4: Nested ngFor loop radiobuttons - click selects every option

Have an app, where i need the 3 x radio-buttons to be independently selected (as a filter functionality).

This is my html:

<div class="dropdown" *ngFor="let type of filterTypes">
    <button type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{{type}}</button>
    <div class="dropdown-menu byrd-shade" aria-labelledby="dropdownMenuButton">
      <div class="radio-group">
        <div class="checkbox-forms gay-flame-forms form-group radio">
          <label class="form-check-label">
              <div *ngFor="let media of filterMedia">
               <input *ngIf="filterMedia"
                      class="form-check-input"
                       type="radio"
                       name="filterMedia"
                       value="1">
               <i aria-hidden="true" class="fa fa-circle"></i>{{media}}
             </div>
           </label>
        </div>
      </div>
    </div>

TS:

 filterTypes: any = ['Media type', 'License type', 'Verification'];
  filterMedia: any = ['All', 'Images', 'Videos'];
  filterLicense: any = ['All, Exclusive, Non-exclusive'];
  filterVerification: any = ['All', 'Verified', 'Non-verified'];

Output:

1 2

Am i approaching this wrong? I need the radiobuttons to be singularly (?) active.

You are giving all the radio inputs same value, ie =1, then how will you distinguish between them. You should bind the value property to a variable depending on your loop iteration like below

 <div *ngFor="let media of filterMedia ; let i = index">
               <input *ngIf="filterMedia"
                      class="form-check-input"
                       type="radio"
                       name="filterMedia"
                       [value]="index">
               <i aria-hidden="true" class="fa fa-circle"></i>{{media}}
  </div>

For three iterations of ngFor, it will create a radio with values 1,2, and 3.That should do the job.

You can wrap the inputs in a form:

<div class="dropdown" *ngFor="let type of filterTypes">
<button type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{{type}}</button>
<div class="dropdown-menu byrd-shade" aria-labelledby="dropdownMenuButton">
  <div class="radio-group">
    <div class="checkbox-forms gay-flame-forms form-group radio">
      <label class="form-check-label">
        <form>
          <div *ngFor="let media of filterMedia">
            <input 
            *ngIf="filterMedia" 
            class="form-check-input" 
            type="radio" 
            name="filterMedia" 
            value="1">
            <i aria-hidden="true" class="fa fa-circle"></i>{{media}}
          </div>
        </form>
      </label>
    </div>
  </div>
</div>

Hopefully I understood your question and this is what you are looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM