我正在尝试编辑表单。我通过localStorage进行了所有更改。添加工作正常,但编辑无效。检查我的TypeScript文件
ngOnInit() {
this.id = this.routeId.snapshot.params['id'];
let emps=this.contactService.getall();
this.contact=emps.find(person => person.id == this.id);
}
onSubmit() {
// I have old and new contact
this.oldContacts = JSON.parse(localStorage.getItem('contacts'));
this.contactService.updateEmployee(this.oldContacts, this.contact);
this.router.navigate(['']);
}
检查我的服务文件:
updateEmployee(oldEmp: any, newEmp: any) {
const emps = JSON.parse(localStorage.getItem('employees'));
console.log({emps});
console.log({oldEmp});
console.log({newEmp});
// emps is old from localstorage
// old is same old data from localstorage
// newEmp is VALUE from my form.
for (let i = 0; i < emps.length; i++) {
if (emps[i].id === oldEmp.id) {
emps[i] = newEmp;
}
}
localStorage.setItem('employees', JSON.stringify(emps));
// this is undefined... ???
}
请尝试传递其类型。例如
var empSubject = new BehaviorSubject
((JSON.parse(localStorage.getItem('employees')));
const emps = empSubject.asObservable();