Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在component.ts,angular2中将数据从一个函数传递到另一个函数_Javascript_Angularjs_Angular_Variables_Components - Fatal编程技术网

Javascript 如何在component.ts,angular2中将数据从一个函数传递到另一个函数

Javascript 如何在component.ts,angular2中将数据从一个函数传递到另一个函数,javascript,angularjs,angular,variables,components,Javascript,Angularjs,Angular,Variables,Components,您好,我正在通过按钮单击“data.someid”传递一些值。我想在另一个按钮单击中获取此“data.someid” html --- <button (click)="click1(data.someid)"></button> <button (click)="click2()"></button> 现在我将检索到的数据存储到“click1”上的this.value,我想在另一个按钮单击中访问它,但是console.log(this.valu

您好,我正在通过按钮单击“data.someid”传递一些值。我想在另一个按钮单击中获取此“data.someid”

html
---
<button (click)="click1(data.someid)"></button>
<button (click)="click2()"></button>
现在我将检索到的数据存储到“click1”上的
this.value
,我想在另一个按钮单击中访问它,但是
console.log(this.value)
显示
未定义的

请有人告诉我如何访问它。

您应该在按钮元素中添加
type=“button”

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

您是否已将该变量声明为类内的局部变量?并确保在单击第二个按钮之前先单击第一个按钮

export class SomeClass {
  value: any;

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

您确定click1()函数中的this.value=数据赋值正确吗?首先尝试在click1()函数中记录值。根据您的代码,函数2依赖于函数1,因为单击函数1时,值变量将被赋值。。因此,在这种情况下,如果您直接尝试函数2,该值将是未定义的。您可以将
type=“button”
添加到您的按钮元素中,然后重试吗?@mayur我想在click2中获取该值,我该如何做。@echonax。。工作很好。。如何在type=“button”上工作。还有其他在“span”点击中使用此功能的方法吗?谢谢,工作很酷。。如果我使用的是span而不是按钮,它就不工作了,还有其他的修复方法吗???@pradeepks它似乎可以很好地使用spans。你能在这本书中复制它吗?嗨,我用span检查了它的工作情况。。。谢谢
export class SomeClass {
  value: any;

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