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

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

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

问题

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

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

<td>
    <input type="text" name="totalFat" value={{totalFat}}>
</td>

在 TypeScript 中,我有:

totalFat = '2.5g';

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

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

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

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

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

amount: 3360
id: 1104
name: "Vitamin A, IU"
source: "Lab"
units: "IU"

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

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

我做错了什么?

英文:

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:

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

In the typescript I have

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:

amount: 3360
id: 1104
name: &quot;Vitamin A, IU&quot;
source: &quot;Lab&quot;
units: &quot;IU&quot;

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

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

What am I doing wrong?

答案1

得分: 0

你可以尝试使用键值数组来实现。类似于以下的方式:

interface Foo {
   [key: string]: number;
}

let foo: Foo[] = [];
foo.push({
  'totalFat': 1337
});

然后,在你的模板中:

<td> <input type="text" name="totalFat" value={{foo[name]}}> </td>

我没有运行它的机会,但可以试试看。
英文:

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

interface Foo {
   [key: string]: number;
}


let foo:Foo[] = [];
foo.push({
  &#39;totalFat&#39;: 1337
});

Then, from your template:

&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

英文:
&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

nutrients = {};
getNutrients() {
    for (let nutrient of this.food.nutrient) {
        this.nutrients[nutrient.name] = nutrient.amount + nutrient.units;
    }
}

.html

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

This is the solution I finally came up with.

.ts

nutrients = {};
  getNutrients() {
    for (let nutrient of this.food.nutrient) {
      this.nutrients[nutrient.name] = nutrient.amount + nutrient.units;
    }
  }

.html

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

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

发表评论

匿名网友

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

确定