I can save, read or update data with localstorage. But im trying to use a Model to display all users (id and userName) with the directive *ngFor. But I get the error: "Only arrays and iterables are allowed". I'm missing something; can someone help me out?
The simple model:
export interface User {
id:number;
userName:string;
}
The HTML:
<input class="form-control" [(ngModel)]="test" >
<button (click)="saveUser(test)" class=" btn btn-danger">SAVE</button>
<div>
<button (click)="readUser(test)" class=" btn btn-danger">Read</button>
</div>
<div>
<ul *ngFor="let user of test">
<li>{{user.id}}</li>
<li>{{user.userName}}</li>
</ul>
</div>
And the component class:
export class Testing implements OnInit {
test: User[]=[];
constructor() { }
saveUser(user:string){
localStorage.setItem('userName', JSON.stringify(user));
console.log(user)
}
readUser(){
console.log(JSON.parse(localStorage.getItem('userName'))
}
*ngForunable to iterate over it. What exactly is that input supposed to do? Add a user to the array?