角度4:Radiobutton值在检查另一个radiobutton后消失

时间:2017-11-06 09:52:28

标签: javascript html angular api typescript

我有3个radiobuttons,用于对API中的不同数据进行排序。在每个单选按钮上,如果我按下另一个单选按钮,则无线电按钮值会消失,如下所示: Thisthis.。 我尝试使用属性绑定[value],但这导致了错误。

TS:

  // Global inputvalues for Radiobuttons
  @Input() all: string; image: string; video: string; true: boolean; false: boolean;


  // Button-event payload
  @Output() mediaButtonSelectionChange: EventEmitter<string> = new EventEmitter<string>();
  @Output() licenseButtonSelectionChange: EventEmitter<string> = new EventEmitter<string>();
  @Output() verifiedButtinSelectionChange: EventEmitter<string> = new EventEmitter<string>();

  sortMedia() {
    this.page = 0;
    this.hitsArray = [];
    this.mediaButtonSelectionChange.emit(this.mediaRadio);
    this.mediaType = this.mediaRadio;
    this.getStories(this.page, this.hits, this.feed, this.mediaType, this.query, this.verifiedType, this.licenseType, this.startDate, this.endDate);

  }

(由于API调用,sortMedia()函数对每个单选按钮排序都有一个。)

HTML:

<div class="container-fluid filter-btn">
  <button class="btn btn-sm gay-flame-btn-light filter-btn dropdown-toggle" type="button dropdown-toggle" data-toggle="dropdown"
    aria-haspopup="true" aria-expanded="false">Media type
    <span *ngIf="mediaType === 'all'" style="color:red;">(All)</span>
    <span *ngIf="mediaType === 'image'" style="color:red;">(Images)</span>
    <span *ngIf="mediaType === 'video'" style="color:red;">(Videos)</span>
  </button>
  <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
    <div class="radio-group">
      <div class="checkbox-forms gay-flame-forms form-group radio">
        <label class="form-check-label">
          <input type="radio" value='all' [(ngModel)]="mediaRadio" (change)="sortMedia()">
          <i aria-hidden="true" [class.active-check]="'all' === 'all'" class="fa fa-circle"></i> All</label>
        <label class="form-check-label">
          <input type="radio" value="image" [(ngModel)]="mediaRadio" (change)="sortMedia()">
          <i aria-hidden="true" class="fa fa-circle"></i> Images</label>
        <label class="form-check-label">
          <input type="radio" value="video" [(ngModel)]="mediaRadio" (change)="sortMedia()">
          <i aria-hidden="true" class="fa fa-circle"></i> Videos</label>
      </div>
    </div>
  </div>
</div>

(此部分每个单选按钮还有3个独立的部分)。

如果按下另一个按钮,如何在每个单选按钮上保留radiobutton值?

1 个答案:

答案 0 :(得分:1)

由@AnteJablan提供,解决方案只是(有点奇怪)向HTML添加名称属性,如下所示:

<input type="radio" name="verification" value="false" [(ngModel)]="verifiedRadio" (change)="sortVerification()">

谢谢Ante!