选中时传递所选复选框的 id(通过 ngFor 加载)

Anu*_*hna 5 html radio-button ngfor angular

       <form name="check-boxes">
          <div *ngFor="let quest of noOfQuestions">
            <div class="form-check form-check-inline" *ngFor="let ans of noOfAnswers">
              <label class="form-check-label">{{ans}}
               <input class="form-check-input" type="checkbox" name="{{quest}}" id="{{quest}}{{type}}" value="{{ans}}"  (change)="getAnswers(id)"> 
              <span class="form-check-sign"></span>
             </label>
            </div>
          </div>
      </form>
Run Code Online (Sandbox Code Playgroud)

角度 componentx.html 中的示例输出: 在此输入图像描述

此代码根据提供的 noOfQuestions 和 noOfAnswers 生成一组复选框。我想在选中复选框时将复选框的特定 id 传递给函数“getAnswer”。ts 文件中传递的参数“id”值表示未定义。getAnswer() 的参数应该是什么来传递所选复选框的 id ???

jep*_*bio 3

如果你通过 $event:

(change)="getAnswers($event)"
Run Code Online (Sandbox Code Playgroud)

那么您应该可以通过以下方式访问打字稿文件中的该输入元素event.target,但检查是否已选中也很重要:

getAnswers(event) {
   if (event.target.checked) {
      console.log('checked: ' + event.target.id);
   } else {
      console.log('unchecked: ' + event.target.id);
   }
}
Run Code Online (Sandbox Code Playgroud)