简体   繁体   中英

how to pass data from one function to another in component.ts , angular2

hi am passing some values on button click "data.someid" i want to get this "data.someid" in another button click

html
---
<button (click)="click1(data.someid)"></button>
<button (click)="click2()"></button>

component.ts

click1(data){
   this.value = data
}
click2(){
  console.log(this.value)
}

Now am storing retrieved data into this.value on "click1" , and i want to access it in the another button click , but console.log(this.value) showing undefined

please someone tell me how to access it.

You should add type="button" to your button elements,

<button type="button (click)="click1(data.someid)"></button>

otherwise the buttons inside the forms will be by default type=submit like you were submitting a form. That is why this refers to something else inside your click scope.

Have you declared the variable as local variable inside class? And make sure that you clicked first button before second one.

export class SomeClass {
  value: any;

  click1(data){
    this.value = data
  }
  click2(){
    console.log(this.value)
  }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM