英文:
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:
<td>
<input type="text" name="totalFat" value={{totalFat}}>
</td>
In the typescript I have
totalFat = '2.5g';
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: "Vitamin A, IU"
source: "Lab"
units: "IU"
And the for loop that tries to add it to the nutrient array is:
for (let nutrientGui of this.myFoodGui.nutrientGuis) {
console.log("nutrientGui=", 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({
'totalFat': 1337
});
Then, from your template:
<td> <input type="text" name="totalFat" value={{foo[name]}}> </td>
Didn’t had a chance to run it, but give it a try.
答案2
得分: 0
英文:
<input type="text" name="Total Fat" value="{{nutrients['Total Fat']}}">
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
<td class="dv">
<input type="text" name="Total lipid (fat)"
value="{{nutrients['Total lipid (fat)']}}">
</td>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论