如何从 TypeScript 数组或映射填充 HTML 表单字段?

huangapple go评论94阅读模式
英文:

How do you fill an html form field from a typescript array or map?

问题

我有一个包含输入元素的HTML表单。我有一个 TypeScript 值数组。我想根据输入字段的名称,将输入字段的值从数组中填充到输入字段中。

我有一个 HTML 表格输入字段,像这样:

  1. <td>
  2. <input type="text" name="totalFat" value={{totalFat}}>
  3. </td>

在 TypeScript 中,我有:

  1. totalFat = '2.5g';

这将在输入字段中正确显示 2.5g。

但如何从数组中获取 totalFat 变量,而不是硬编码它?

编辑:
我有一个 nutrientGuis 数组:NutrientGui[]。NutrientGui 是一个对象 {name:string,amount:number}

创建 nutrientGuis 数组时出现错误 "Cannot set properties of undefined (setting 'Vitamin A, IU')"。

这是源中的第一条记录。以下是该记录:

  1. amount: 3360
  2. id: 1104
  3. name: "Vitamin A, IU"
  4. source: "Lab"
  5. units: "IU"

尝试将其添加到 nutrient 数组的 for 循环如下:

  1. for (let nutrientGui of this.myFoodGui.nutrientGuis) {
  2. console.log("nutrientGui=", nutrientGui);
  3. this.nutrientGuis[nutrientGui.name] = nutrientGui.amount;
  4. }

我做错了什么?

英文:

I have an html form with an input element. I have an array of typescript values. I want to populate the value of the input field with a value from the array, depending on the name of the input field.

I have an html table input input field, like this:

  1. &lt;td&gt;
  2. &lt;input type=&quot;text&quot; name=&quot;totalFat&quot; value={{totalFat}}&gt;
  3. &lt;/td&gt;

In the typescript I have

  1. totalFat = &#39;2.5g&#39;;

This correctly displays 2.5g in the input field.

But how to get the totalFat variable from an array, instead of hard-coding it?

EDIT:
I have an array nutrientGuis: NutrientGui[]. NutrientGui is an object {name:string,amount:number}.

Creating the nutrientGuis array is failing with "Cannot set properties of undefined (setting 'Vitamin A, IU')"

That is the first record in the source. Here is that record:

  1. amount: 3360
  2. id: 1104
  3. name: &quot;Vitamin A, IU&quot;
  4. source: &quot;Lab&quot;
  5. units: &quot;IU&quot;

And the for loop that tries to add it to the nutrient array is:

  1. for (let nutrientGui of this.myFoodGui.nutrientGuis) {
  2. console.log(&quot;nutrientGui=&quot;, nutrientGui);
  3. this.nutrientGuis[nutrientGui.name] = nutrientGui.amount;
  4. }

What am I doing wrong?

答案1

得分: 0

  1. 你可以尝试使用键值数组来实现。类似于以下的方式:
  2. interface Foo {
  3. [key: string]: number;
  4. }
  5. let foo: Foo[] = [];
  6. foo.push({
  7. 'totalFat': 1337
  8. });
  9. 然后,在你的模板中:
  10. <td> <input type="text" name="totalFat" value={{foo[name]}}> </td>
  11. 我没有运行它的机会,但可以试试看。
英文:

You might be able to do that using a key-value array. Something like:

  1. interface Foo {
  2. [key: string]: number;
  3. }
  4. let foo:Foo[] = [];
  5. foo.push({
  6. &#39;totalFat&#39;: 1337
  7. });

Then, from your template:

  1. &lt;td&gt; &lt;input type=&quot;text&quot; name=&quot;totalFat&quot; value={{foo[name]}}&gt; &lt;/td&gt;

Didn’t had a chance to run it, but give it a try.

答案2

得分: 0

英文:
  1. &lt;input type=&quot;text&quot; name=&quot;Total Fat&quot; value=&quot;{{nutrients[&#39;Total Fat&#39;]}}&quot;&gt;

we use double quotes to enclose the entire attribute value, and single quotes to enclose the object key inside the attribute value.

答案3

得分: 0

以下是翻译好的部分:

.ts

  1. nutrients = {};
  2. getNutrients() {
  3. for (let nutrient of this.food.nutrient) {
  4. this.nutrients[nutrient.name] = nutrient.amount + nutrient.units;
  5. }
  6. }

.html

  1. <td class="dv">
  2. <input type="text" name="Total lipid (fat)"
  3. value="{{nutrients['Total lipid (fat)']}}">
  4. </td>
英文:

This is the solution I finally came up with.

.ts

  1. nutrients = {};
  2. getNutrients() {
  3. for (let nutrient of this.food.nutrient) {
  4. this.nutrients[nutrient.name] = nutrient.amount + nutrient.units;
  5. }
  6. }

.html

  1. &lt;td class=&quot;dv&quot;&gt;
  2. &lt;input type=&quot;text&quot; name=&quot;Total lipid (fat)&quot;
  3. value=&quot;{{nutrients[&#39;Total lipid (fat)&#39;]}}&quot;&gt;
  4. &lt;/td&gt;

huangapple
  • 本文由 发表于 2023年3月21日 01:58:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793747-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定