传递id作为函数的参数

时间:2017-10-10 12:45:05

标签: javascript angular

<div class="row">
  <div class="text" id="radioButtonWrapper" style="display:inline-block;">Gender</div>
  <div class="data">
    <div id="genderSection" style="display:inline-block;">
      <radiobutton [(model)]="gender" name="gender" value="Female" (modelChange)="reciveRadioButtonVal($event, IdasParameter )" required>Female</radiobutton>
      <radiobutton [(model)]="gender" name="gender" value="Male" (modelChange)="reciveRadioButtonVal($event, IdasParameter)" required>Male</radiobutton>
    </div>
  </div>
</div>

可以传递功能一些ID。在这个例子中,我想传递id =&#34; radioButtonWrapper&#34;在函数reciveRadioButtonVal($event, IdasParameter )中 其中IdasParameter =&gt; &#34; radioButtonWrapper&#34; 我不想使用并将其作为字符串reciveRadioButtonVal($event,radioButtonWrapper)传递?

2 个答案:

答案 0 :(得分:2)

您可以使用angular的元素选择器传递它,在单选按钮上设置属性

<div class="row">
  <div class="text" id="radioButtonWrapper" #radioButtonWrapper style="display:inline-block;">Gender</div>
  <div class="data">
    <div id="genderSection" style="display:inline-block;">
      <radiobutton [(model)]="gender" name="gender" value="Female" (modelChange)="reciveRadioButtonVal($event,radioButtonWrapper)" required>Female</radiobutton>
      <radiobutton [(model)]="gender" name="gender" value="Male" (modelChange)="reciveRadioButtonVal($event,radioButtonWrapper)" required>Male</radiobutton>
    </div>
  </div>
</div>

它将作为角元素传递给你的函数,所以在你的函数中你应该得到它...

reciveRadioButtonVal(event, radiobutton){
    console.log(radiobutton.nativeElement);// this is the radio button
    console.log(radiobutton.nativeElement.id);// this is the id
}

获得元素后,您可以访问其所有属性..

答案 1 :(得分:2)

创建引用而不是id。那是#radioButtonWrapper并将radioButtonWrapper传递给函数(不作为字符串。作为变量)

<div class="row">
  <div class="text" #radioButtonWrapper style="display:inline-block;">Gender</div>
  <div class="data">
    <div id="genderSection" style="display:inline-block;">
      <radiobutton [(model)]="gender" name="gender" value="Female" (modelChange)="reciveRadioButtonVal($event, radioButtonWrapper )" required>Female</radiobutton>
      <radiobutton [(model)]="gender" name="gender" value="Male" (modelChange)="reciveRadioButtonVal($event, radioButtonWrapper)" required>Male</radiobutton>
    </div>
  </div>
</div>