【发布时间】:2020-11-24 09:14:42
【问题描述】:
当我尝试遍历 this.data.members 并在 this.addedUsers 上的 forEach 中执行一些操作时,我收到 TypeError: Cannot read property未定义的“已添加用户”。我可以在 foarEach 之外访问 this.data.members。
export class AddgroupComponent implements OnInit {
public addedUsers: any[] = [];
constructor(
@Inject(MAT_DIALOG_DATA) public data: any
) { }
ngOnInit(): void {
this.data.members.forEach(function (item) {
console.log(this.addedUsers)
});
}
}
}
【问题讨论】:
-
thisinside JSfunction指的是函数的作用域。使用箭头函数this.data.members.forEach(item => {...或bind函数来引用类成员变量。 -
感谢您的回复..使用第一个箭头函数技术工作@MichaelD
标签: angularjs angular typescript typeerror