gpt4 book ai didi

javascript - Aurelia - 在数据绑定(bind)中使用变量作为属性名称

转载 作者:数据小太阳 更新时间:2023-10-29 04:38:27 28 4
gpt4 key购买 nike

使用Aurelia,是否可以使用变量名动态引用模型对象的属性名?

Javascript:

dow = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday'];
test = {
monday: false,
tuesday: false,
wednesday: false,
thursday: false,
friday: false,
};

HTML:

<label repeat.for="day of dow"><input type="checkbox" class="form-control" checked.bind="test[day]" />${day}</label>

这导致:“TypeError:obj 未定义”

最佳答案

根据 Aurelia 文档:

Each item that is being repeated by the repeat attribute has severalspecial contextual values available for binding:

$parent - At present, the main view model's properties and methods arenot visible from within the repeated item. We hope to remedy this inan update soon. For the mean time, you can access that view-model with$parent.

$index - The index of the item in the array.

$first - True if the item is the first item in the array.

$last - True if the item is the last item in the array.

$even - True if the item has an even numbered index.

$odd - True if the item has an odd numbered index.

它在更新 HTML 以引用 parent.new_deal[day] 后起作用:

<label repeat.for="day of dow"><input type="checkbox" class="checkbox-inline" checked.bind="$parent.new_deal[day]" />${day}</label>

关于javascript - Aurelia - 在数据绑定(bind)中使用变量作为属性名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32322459/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com