【发布时间】:2018-10-22 19:53:18
【问题描述】:
您好,我想知道如何在嵌套函数中使用组件的变量。
这是一个例子:
export class AppComponent implements OnInit {
name = ['Angular 6','Angular5','Angular4','Angular2'];
isexist: string[]=[];
ifExist(text){
var that= this;
console.log("first ",this);
var test="test";
let exist= this.name.map(function (elm){
if(elm==text) {
console.log(this);
this.isexist.push(elm); // works with that.isexist.push(elm);
}
})
}
ngOnInit() {
this.ifExist('Angular 6');
}
这是我在浏览器开发工具中得到的结果
first AppComponent {name: Array(4), namev: "helo", isexist: Array(1)};
second undefined
我有一些问题
如何在不使用箭头功能的情况下访问isexist?
为什么第二个this 不包含test 元素?
【问题讨论】:
标签: javascript typescript ecmascript-6 ecmascript-5