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