单选按钮更改未触发

时间:2019-04-22 14:39:47

标签: angular

由于某种原因,我的(change)事件没有触发...知道为什么吗?

HTML

<div class="row" style="padding-top: 0.5em; padding-bottom: 2em">
  <div class="col-md-12 text-left">
    {{ selectedCategory }}
    <div class="custom-controls-stacked">
      <div class="custom-control custom-radio">
        <input type="radio" class="custom-control-input" [value]="'Web, Software and Mobile Development'" name="step1" [(ngModel)]="selectedCategory" (change)="onSelectedCategoryChange($event)">
        <label class="custom-control-label">Web, Software and Mobile Development</label>
      </div>
      <div class="custom-control custom-radio">
        <input type="radio" class="custom-control-input" [value]="'Graphics Design'" name="step1" [(ngModel)]="selectedCategory" (change)="onSelectedCategoryChange($event)>
        <label class="custom-control-label">Graphics Design</label>
      </div>
      <div class="custom-control custom-radio">
        <input type="radio" class="custom-control-input" [value]="'Writing and Translation'" name="step1" [(ngModel)]="selectedCategory" (change)="onSelectedCategoryChange($event)>
        <label class="custom-control-label">Writing and Translation</label>
      </div>
    </div>
  </div>
</div>

JavaScript

  onSelectedCategoryChange(event: any) {
    console.log(`it works with... ${event}`);
  }

2 个答案:

答案 0 :(得分:1)

使用 (ngModelChange) 并传递变量

 <input type="radio" class="custom-control-input" [value]="'Web, Software and Mobile Development'" name="step1" [(ngModel)]="selectedCategory" (ngModelChange)="onSelectedCategoryChange(selectedCategory)">

答案 1 :(得分:0)

您应该使用ngModelChange。另外,请查看控制台。也许您的函数也会出错。