0

I have a component with a variable say 'id' and in this component i'm calling a service that's take that variable(id) as parameter, in my template I assign the value of a radio Button to the variable(id).. the problem is ..I'm calling my Service in "ngOnInit()", so it doesn't take the value of the radio button , but takes the initial value of the "id".. Here is my code:

in my component:

 import { Component, OnInit } from '@angular/core';
 import { BartersService } from '../../core/services/barters.service'; 
 export class BarterViewComponent implements OnInit {
 id:string="";
 allCategories ;
 constructor( private barServ: BartersService) { }
 ngOnInit() {
 this.barServ.findByPage(this.id)
 .subscribe(respone => {
 this.allCategories = respone.data;
 });
 }
 onSelectionChange(val) {
 this.id = val;
 }
}

in my Template :

<div *ngFor="let cat of allCategories ; let i=index;"> 
 <input style="margin-bottom: 10px;" type="radio" #R 
 (change)="onSelectionChange(R.value )" name="rad" value="cat['id']"> 
 {{cat['name']}}
</div>

Target:

the target is updating the view based on the selection of the radio button.

asked Nov 14, 2017 at 12:39
1
  • What is the expected behavior? Commented Nov 14, 2017 at 12:45

2 Answers 2

2

Change this

value="cat['id']"

to

[value]="cat['id']" or value="{{cat['id']}}"

answered Nov 14, 2017 at 12:45
Sign up to request clarification or add additional context in comments.

Comments

0

You can try this:

export class BarterViewComponent implements OnInit {
 id:string="";
 allCategories ;
 constructor( private barServ: BartersService) { }
 ngOnInit() {
 refreshCategories()
 }
 onSelectionChange(val) {
 this.id = val;
 refreshCategories()
 }
 refreshCategories() {
 this.barServ.findByPage(this.id).subscribe(respone => {
 this.allCategories = respone.data;
 });
 }
}
answered Nov 14, 2017 at 12:48

1 Comment

you can also pass the id as a param to refreshCategories().

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.