i m stuck on the update of the user input how do i edit or update the default value entering User ID //user service import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class UserService {
userArray : any[] =[];
constructor() { }
public getUsers() : any[]{
return this.userArray;
}
public save(obj : any):void {
const lastElement = this.userArray[this.userArray.length - 1];
if(lastElement !== undefined){
obj.id = lastElement.id+1;
}
else{
obj.id = 1;
}
this.userArray.push(obj);
}
public delete(id: number) : void {
for(let i = 0; i < this.userArray.length; i++) {
let user = this.userArray[i];
if(user.id == id) {
this.userArray.splice(i,1);
}
}
}
public update(id:number):void {
}
}
//this below is store.component.ts this is where i store the user inputs export class StoreFormComponent implements OnInit {
constructor(private builder : FormBuilder,private service : UserService, private
_router:Router) { }
ngOnInit(): void {
}
userForm : FormGroup = this.builder.group({
id:[''],
name : ['',[Validators.required,Validators.minLength(2), Validators.maxLength(10)]],
gender :['',[Validators.required]],
phone:['',[Validators.required]],
email:['',[Validators.required]],
address: this.builder.group({
state:['',[Validators.required]],
city:['',[Validators.required]],
pin:['',[Validators.required]]
})
});
saveForm(){
this.service.save(this.userForm.value)
this._router.navigate(["success",this.userForm.value.id]);
console.log(this.userForm.value)
}
clear(){
this.service.save({})
}
}
And this //problem update i want to edit the user input value using specific user id like when i put user id 1 update the default value or edit the new value export class UpdateComponent implements OnInit {
constructor(private service: UserService) { }
ngOnInit(): void {
}
id: FormControl = new FormControl('');
update() {
this.service.delete(this.id.value);
}
}
[this is upload picture][1][output pciture][2]
[1]: https://i.stack.imgur.com/O4Itf.jpg
[2]: https://i.stack.imgur.com/UQAHe.jpg