[英]How can I get all the values of the inputs and put them in the right place in my object (localStorage)?
How can I get all the values of the inputs and put them in the right place in my object (localStorage) without having to retrieve them one by one as in the code below? 如何获取输入的所有值并将它们放在我的对象(localStorage)中的正确位置,而不必像下面的代码一样逐一检索它们?
This is my function for update informations: 这是我的更新信息功能:
updateContact() {
if (this.onSubmit()) {
this.eleveService.apiUpdateEleve(this.token, this.registerForm.get("firstName").value, this.registerForm.get("lastName").value
).subscribe((data: any) => {
// récupération des informations du formulaire
this.student['contact']['us_firstname'] = this.registerForm.get("firstName").value;
this.student['contact']['us_lastname'] = this.registerForm.get("lastName").value;
// this.language = this.student['us_lang']
this.student['us_lang'] = this.lang
this.translate.use(this.student['us_lang']);
// MAJ de l'objet
localStorage.setItem("student", JSON.stringify(this.student));
console.log('maj', this.student)
this.navCtrl.navigateRoot('/e-tabs/explorer');
})
}
}
This is my Object : 这是我的对象:
OBJECT :
contact:
country: {id: "BE", name: "Belgique"}
login: "last@last.be"
newsletter: "1"
prest_feedback: "1"
us_address: "Rue des polders 13"
us_city: "Uccle"
us_email: "test@test.be"
us_firstname: "Munir"
us_gsm: "0485001128"
us_id: "5c9b35d8b4dd1"
us_lang: "fr"
us_lastname: "Nahman"
us_zip: "1180"
[Object][1]
[Front-end of the page][2]
[1]: https://i.stack.imgur.com/SmxSZ.jpg
[2]: https://i.stack.imgur.com/oknZd.jpg
Since they are all key-value pairs, The shortest way to do it will be to stringify and store the entire object inside your localStorage. 由于它们都是键值对,因此最简单的方法是将整个对象进行字符串化并存储在localStorage中。
localStorage.setItem('contactObject', JSON.stringify(yourObject));
And when you want to retrieve the object from localStorage, you can simply retrieve and parse it back as an object. 当您想从localStorage检索对象时,您可以简单地将其作为对象进行检索和解析。
const retreivedContactObject = JSON.parse(localStorage.getItem('contactObject'));
console.log(retreivedContactObject);
EDIT: I recommend you to set your registerForm
FormControl names to be the same as your student
object. 编辑:我建议您将
registerForm
FormControl名称设置为与您的student
对象相同。
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
.
.
student = {
contact: {
custName: undefined,
custAddress: undefined,
custTown: undefined,
.
.
}
};
constructor(private formBuilder: FormBuilder) { }
registerForm: FormGroup = this.formBuilder.group({
custName: [null, Validators.required],
custAddress: [null, Validators.required],
custTown: [null, Validators.required],
.
.
},
Once you have set your formControls to have the same name as your student object, 将formControls设置为与学生对象同名后,
you can simply update your student
this way: 你可以这样简单地更新你的
student
:
this.student.contact = {...this.registerForm.value};
//console.log(this.student);
var student = { contact: { name: "hello", class: "hello", age: "hello", gender: "hello", number: "hello", username: "hello", email: "hello", home: "hello", mobile: "hello" } }; for ( var property in student.contact ) { console.log(property , student.contact[property]) }
This will allow you to get property name and its value. 这将允许您获取属性名称及其值。 then you can do something like this.
然后你可以做这样的事情。 if i understand properly
如果我理解得当
for ( var property in this.student.contact ) {
this.student['contact'][property] = this.registerForm.get(property).value;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.