2

I want to get relevant selections when radio button clicks. my relevant files are as below.

app.component.html

 <div class="col-md-3">
 <b>Select Catogory</b><br>
  <input type="radio" name="colors" [(ngModel)]="typing" (click)= "checktype(typing)" value="Agent">Agent<br>
  <input type="radio" name="colors" [(ngModel)]="typing" (click)= "checktype(typing)" value="Hospital" >Hospital
  <select>                                                                                                                                          
    <option [(value)]="agent" *ngFor="let agent of agents">{{agent}}</option>
  </select>
</div>

app.component.ts

checktype(typing){
   console.log(typing)
   if(typing=="Agent"){
    this.agents=["amila","kasun", "ayesh"]
    console.log(typing)
  }else{
   this.agents=["hemas","general", "Asiri"]
   console.log(typing)
   }
  }

But when I click Agent radio button It display relevant data for Hospital radio button. I can't figure out what is the wrong with code. Any one can help me? Are there any mistakes done by me?

3
  • Please fix your indentation, your code as it stands is not readable. Commented Oct 30, 2019 at 7:39
  • 1
    can you explain But when I click Agent radio button It display relevant data for Hospital radio button Commented Oct 30, 2019 at 7:43
  • 1
    Could you try by giving local reference (#someName) to your radios and pass the value in the checktype function? Commented Oct 30, 2019 at 7:46

2 Answers 2

2

Replace (click) by (ngModelChange)

Try like this:

Working Demo

<input type="radio" name="colors" [(ngModel)]="typing" (ngModelChange)= "checktype(typing)" value="Agent">Agent<br>
<input type="radio" name="colors" [(ngModel)]="typing" (ngModelChange)= "checktype(typing)" value="Hospital" >Hospital
Sign up to request clarification or add additional context in comments.

2 Comments

You are welcome. I answered this first and then got 2 downvotes, not sure why the hatred!
Why use two-way data binding for input control at all? A simple solution would be to pass a custom value to the event handler, Something like this
2

Use change event instead

<input type="radio" name="colors" [(ngModel)]="typing" (change)= "checktype(typing)" value="Agent">Agent<br>
  <input type="radio" name="colors" [(ngModel)]="typing" (change)= "checktype(typing)" value="Hospital" >Hospital

demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.