[英]Variable inside of a component in Angular 2
I'll try to explain my question through an example. 我将尝试通过一个例子来解释我的问题。
If I have something like this in my new component: 如果我在新组件中有类似的内容:
... implements OnInit {
constructor( ... ) { }
variable = "";
ngOnInit(): void {
...
}
Then I can use my variable
inside of the ngOnInit with this.
然后,我可以在ngOnInit中使用我的
variable
this.
, like so: ,就像这样:
this.variable = '1';
But how can I use my variable inside of a child function of the ngOnInit? 但是,如何在ngOnInit的子函数中使用我的变量? For example:
例如:
ngOnInit(): void {
...
// here I want to use another function with my `variable`
// something like:
// myFunc() {
// this.variable = '2';
// }
...
}
Thank you. 谢谢。
just use arrow function and you're good as it automatically bind itself to this
. 只需使用箭头函数,你会很好,因为它会自动绑定到
this
。
ngOnInit(): void {
let somefunc: any = () => {
this.variable = "got here";
};
}
hope that helps 希望能有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.