我在以下内容中遗漏了什么吗:
<select
id={{@field}}
name={{@field}}
{{on "change" this.sendSelect}}
>
<option value="null">Select one ... </option>
{{#each this.data as |item|}}
{{log "in record.hbs, curVal=" @curVal}}
{{log "in record.hbs, item.id=" item.id}}
{{debugger}}
<option
value={{item.id}}
selected={{fn this.areEqual @curVal item.id}}
>{{item.name}}</option>
{{/each}}
</select>
首次加载时未识别选定的选项。{{log}}语句显示其中一条记录的@curVal和item.id的值相等。使用{{调试器}}和
typeof get(item.id)
@curVal的相同之处表明它们都是字符串。
我最初使用烬真相助手,与
{{eq @curVal item.id}}
但这永远不会选择任何东西。所以我写了一个本地函数:
@action
areEqual(curval, itemId) {
console.log('typeof curval', typeof curval);
console.log('typeof itemId', typeof curval);
return curval == itemId;
}
但它似乎从未被调用过。我做错了什么?
尝试删除fn
selected={{this.areEqual @curVal item.id}}
fn
部分应用参数,然后返回未调用的函数。
语法:
{{someFunction arg1 arg2}}
立即调用someFunction
文件:
- https://guides.emberjs.com/release/upgrading/current-edition/action-on-and-fn/#toc_the-fn助手
- https://api.emberjs.com/ember/4.6/classes/Ember.Templates.helpers/methods/fn?anchor=fn
- 语法备忘单:https://cheatsheet.glimmer.nullvoxpopuli.com/docs/templates