in my component angular call services I need to wait and change before move to execute all code any solution?
read(idPos:string){
var choice = 0;
this.menuItemsService.getAllDataMenuItemsByParentId(idPos).subscribe(data=>
{
choice = 1; // change Value to 1
if(data.length !=0){
this.data =data;
}
console.log(choice)
})
console.log(choice); // print 0 no change to =1
Basically, you should not be putting any code below an asynchronous call (subscribe) as you have in your example.
Instead, consider this:
read(idPos:string){
var choice = 0;
this.menuItemsService.getAllDataMenuItemsByParentId(idPos).subscribe(data=>
{
choice = 1; // change Value to 1
if(data.length !=0){
this.data =data;
}
console.log(choice)
this.continueRead(choice);
})
// console.log(choice); // <<< remove this line.
}
continueRead(choice: number) {
console.log(choice); // <<< will print 1
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.