You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ember.js中href属性作用域绑定失效问题求助

Fixing Ember.js Component href Binding Issue

Hey there, I see the problem you're facing. The issue is that when you pass href="/fruits/{{fruit.id}}" to your list-item component, Ember isn't parsing the inner {{fruit.id}} expression—it's just passing the literal string "/fruits/{{fruit.id}}" instead of interpolating the fruit's ID. That's why your href isn't resolving to /fruits/1 like you expect.

Here are two easy fixes to get this working:

1. Use the concat helper (compatible with all Ember versions)

The concat helper is perfect for combining static strings with dynamic values. Update your home.hbs code to use it when passing the href:

<ul>
{{#each fruits as |fruit|}}
  {{list-item href=(concat "/fruits/" fruit.id) item=fruit onitemclick=(action "handleFruitsDetail")}}
{{else}}
  <li>Loading!</li>
{{/each}}
</ul>

This will properly combine "/fruits/" with the fruit.id value to create the correct URL.

2. Use ES6 template literals (Ember 3.10+)

If you're using Ember 3.10 or newer, you can use template literals directly in your template for cleaner syntax:

<ul>
{{#each fruits as |fruit|}}
  {{list-item href=`/fruits/${fruit.id}` item=fruit onitemclick=(action "handleFruitsDetail")}}
{{else}}
  <li>Loading!</li>
{{/each}}
</ul>

The backticks ` tell Ember to evaluate the ${fruit.id} part as a dynamic value.

Good news—your list-item.hbs template is already correct! The <a href="{{href}}"> line will properly bind the computed href value we're now passing from the parent.

内容的提问来源于stack exchange,提问作者Abraham Gnanasingh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:35:53