Hi
I have below data in Local Storage and i want to save data is studentObj. I am using Rective form
studentObj: Student = new Student();
studentList:Student[] = [];
get f(){
return this.form.controls;
}
Thanks
Hi
I have below data in Local Storage and i want to save data is studentObj. I am using Rective form
studentObj: Student = new Student();
studentList:Student[] = [];
get f(){
return this.form.controls;
}
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vishal JoshiRank #377 · 6.7K reputationPosted May 15, 2024, 2:15 AM EDT
Hello Ramco
You can use the localStorage to store the data in local storage.
please check-out the below code.
To set the value:
JSON.stringify() is needed if you are storing an object.
localStorage.setItem('form-data', JSON.stringify(this.testform.value));To get the stored object from localStorage:
let formValue = JSON.parse(localStorage.getItem('form-data'))Thanks
Vishal Joshi
Jignesh KumarRank #91 · 22.9K reputationPosted May 15, 2024, 1:04 AM EDT
Hello Ramco,
import { Component, OnInit } from '@angular/core'; import { FormBuilder, FormGroup } from '@angular/forms'; class Student { // Define all your form properties of Student here constructor( public id?: number, public name?: string, public age?: number ) {} } @Component({ selector: 'app-student', templateUrl: './student.component.html', styleUrls: ['./student.component.css'] }) export class StudentComponent implements OnInit { form: FormGroup; studentObj: Student = new Student(); studentList: Student[] = []; constructor(private fb: FormBuilder) { this.form = this.fb.group({ // Define your form controls here with all properties , e.g.: id: [''], name: [''], age: [''] }); } ngOnInit(): void { this.loadStudentData(); } get f() { return this.form.controls; } loadStudentData(): void { // Load data from local storage name with "studentObj" const studentData = localStorage.getItem('studentObj'); const studentListData = localStorage.getItem('studentList'); if (studentData) { this.studentObj = JSON.parse(studentData); // Set the form values with the data from local storage this.form.patchValue(this.studentObj); } if (studentListData) { this.studentList = JSON.parse(studentListData); } } saveStudentData(): void { // Get data from the form and save it to studentObj this.studentObj = this.form.value; // Save studentObj and studentList to local storage localStorage.setItem('studentObj', JSON.stringify(this.studentObj)); localStorage.setItem('studentList', JSON.stringify(this.studentList)); } addStudentToList(): void { // Now you can add the current studentObj to the studentList this.studentList.push(this.studentObj); // Save the updated studentList to local storage with new student details localStorage.setItem('studentList', JSON.stringify(this.studentList)); } }Naimish MakwanaRank #324 · 7.7K reputationPosted May 15, 2024, 2:44 AM EDT
If you want to save data in
studentObjand you are using Reactive Forms, you can do something like this:import { FormGroup, FormControl } from '@angular/forms'; // Assuming you have a Student class export class Student { name: string; age: number; // add other properties as needed } // Your component export class YourComponent implements OnInit { studentObj: Student = new Student(); studentList: Student[] = []; form: FormGroup; ngOnInit() { this.form = new FormGroup({ name: new FormControl(''), age: new FormControl(''), // add other controls as needed }); } get f() { return this.form.controls; } saveData() { this.studentObj = this.form.value; // This will save form data to studentObj this.studentList.push(this.studentObj); // This will add the studentObj to studentList } }In the above code,
ngOnInitis used to initialize the form withFormControlfor each field. ThesaveDatamethod is used to save the form data tostudentObjand then pushstudentObjtostudentList.Remember to call
saveDatamethod when you want to save the data, for example, on a button click.Please replace
'name','age'and other properties with your actual Student class properties. Also, don’t forget to importFormGroupandFormControlfrom@angular/forms.thanks