我制作了一个函数,其中它接受值并搜索数组并返回键。我尝试将其记录在订阅函数之外,并且可以正常运行,但是一旦将其放入内部,它就会返回未定义状态。
private mstat: Array<object> = [
{'key' : 1, 'val': 'Single'},
{'key' : 2, 'val': 'Married'},
{'key' : 3, 'val': 'Widowed'},
{'key' : 4, 'val': 'Divorced'}
]
this.userService.getEmployeeById(+empId)
.subscribe( data => {
this.editForm.setValue(data,this.mstat);
this.editForm.patchValue(
{
emp_status: this.mStatHelper(data.emp_status),
emp_department: data.department.dept_id,
});
console.log( this.editForm.value);
});
this.getDepartments();
}
mStatHelper(mstat: string){
this.mstat.forEach(function(m){
if(m.val == mstat) return m.key;
});
}