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.
-
What is the expected behavior?Günter Zöchbauer– Günter Zöchbauer2017年11月14日 12:45:44 +00:00Commented Nov 14, 2017 at 12:45
2 Answers 2
Change this
value="cat['id']"
to
[value]="cat['id']" or value="{{cat['id']}}"
answered Nov 14, 2017 at 12:45
Vivek Doshi
58.8k12 gold badges120 silver badges126 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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
Mohamed Gara
2,9443 gold badges16 silver badges21 bronze badges
1 Comment
Mohamed Gara
you can also pass the id as a param to refreshCategories().
lang-js