Home > database >  passing a value from a method to another method
passing a value from a method to another method

Time:01-05

I try to pass a value that I grab from button

<button mat-button type="button" (click)="getOrderid(booking?.order_id)">Accept Job</button>

I do the console log on second method, it didn't return any value inside my second method here is both my method

getOrderid(order_id?: any){
    console.log (order_id);
  }
signupdatebooking() {
    var user_id = localStorage.getItem('user_id');
    console.log (order_id);
    const body = {
      id: ,
      sprayer_id: user_id,

    };
}

CodePudding user response:

When you have a method like this:

getOrderid(order_id?: any){
    console.log (order_id);
  }

The argument, order_id is local to the method and will not be available to code outside of the getOrderid method.

When you try to access the the order_id variable inside the signupdatebooking() it looks at variables defined locally to that method and sees none, thus cannot access it.

Based on your HTML code, I would probably just access the booking.order_id directly, like this:

signupdatebooking() {
    var user_id = localStorage.getItem('user_id');
    console.log (this.booking.order_id);
    const body = {
      id: ,
      sprayer_id: user_id,
    };
}

However if you need a high level copy of the booking.order_id inside the component class, you could first create a variable in the component:

order_id;

Then in you're method do something like this:

getOrderid(order_id?: any){
  this.order_id = order_id;
  console.log (order_id);
}

And you can access that inside the signupdatebooking() method:

signupdatebooking() {
    var user_id = localStorage.getItem('user_id');
    console.log (this.order_id);
    const body = {
      id: ,
      sprayer_id: user_id,
    };
}
  •  Tags:  
  • Related