简体   繁体   English

如何获取输入的所有值并将它们放在我的对象(localStorage)中的正确位置?

[英]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.

相关问题 如何从多个 arrays 中获取每个 object 并将它们全部放入一个数组中 - How can I get each object from multiple arrays and put them all into one array 如何从本地存储中获取特定数据,以便在 JavaScript 中以模态显示它们? - How do I get specific data from my localstorage so I can show them in a modal in JavaScript? 如何获得 2 个输入的值,计算它们并将结果放入另一个 - How can I get the value of 2 inputs, calculate them and put the result in another 如何从多个输入中获取数据并将其放入localStorage? 反应 - How to get data from multiple inputs and put it to the localStorage? React 如何获取一个 object 的值并将它们放入另一个 object - How can I take a values of one object and put them into another object 如何才能将所有值显示在桌面上? - How can I get all my values to be displayed on my table? 如何影响存储在 localStorage 上的 object 值而不覆盖它们? - How do I affect object values stored on localStorage without overwriting them? "如何在本地存储中获取对象的特定元素值" - How can I get a specific element value of object in localstorage 想要从表单获取所有文本输入并将其置于JavaScript警报中。 为什么我的代码不起作用? - Want to get all the text inputs from a form and put them in a JavaScript alert. Why isn't my code working? 如何从页面获取所有内容,包括带有按钮的输入值? - How can I get all contents from page including values from inputs with a button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM