Angular 2中的组件内部变量
问题描述:
我会尝试通过一个示例来解释我的问题。Angular 2中的组件内部变量
如果我有这样的事情在我的新组件:
... implements OnInit {
constructor(...) { }
variable = "";
ngOnInit(): void {
...
}
那么我可以用我的variable
的ngOnInit内有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";
};
}
希望帮助