【发布时间】:2018-02-22 05:34:21
【问题描述】:
我正在处理 angular 2 并且在 subscribe() 函数之外的变量出现未定义错误。employee 变量 id undefined 在 subscribe() 函数之外。请帮助我。谢谢。下面是代码
export class DetailsComponent implements OnInit {
employee;
constructor(private _location:Location, private _empService:EmpService, private route:ActivatedRoute) { }
ngOnInit() {
//let params:any = this.route.snapshot.params;
this.editEmployee(6); //I have tried to pass static value
console.log(this.employee); //Here this is undefined
}
backClicked() {
this._location.back();
}
editEmployee(id) {
this._empService.getEmployee(id).subscribe((employee)=> {
this.employee = employee[0];
console.log(employee[0]); //This have below output in console
//{id: "6", emp_name: "Mohit", emp_email: "mohit@ommzi.com", emp_phone: "9800000", emp_status: "y"}
})
}
}
【问题讨论】:
标签: angularjs-scope angular2-services