我想在我的Angular 4应用中存储和检索localstorage的数组时,我错过了一些基本的东西。
该应用程序从服务器中检索数组中的"任何"键入变量,称为atodo as:
{"qoption":"Trap Surveyor (<em>Competency for CM OAP</em>)"}
并存储在LocalStorage中:
localStorage.setItem('aToDo',JSON.stringify(this.aToDo));
它从LocalStorage中检索为:
this.aToDo = JSON.parse(localStorage.getItem('aToDo'));
this.atodo显示的台:
{"qoption":"Trap Surveyor (<em>Competency for CM OAP</em>)"}
但是,到这个阶段,事物被塞满了登录this.atodo.qoption会产生"未定义",并且用于列出数组内容的 *ngfor会生成可恶的错误:
Error: Cannot find a differ supporting object '{"qoption":"Trap Surveyor (<em>Competency for CM OAP</em>)"}' of type 'string'.
我的错误在哪里?谢谢/汤姆
两件事:
- Console.log默认情况下将"未定义"与输出一起输出,因为它不是返回功能。那是正常的。
- atodo是一个对象,而不是数组。您不能在对象上使用ngfor。