【问题标题】:Defining an ID for html element in Emberjs/handlebars fails在 Emberjs/handlebars 中为 html 元素定义 ID 失败
【发布时间】:2014-04-03 14:37:02
【问题描述】:

我这里有以下问题:

<ul class="aClass">
  {{#if something}}
  <li>{{#link-to "blub" bind-attr data-testid=aID}}{{loc "blub"}}{{/link-to}}</li>
  {{/if}}
</ul>

所以我想在生成的元素中有一个元素(链接到&lt;a href=""&gt;...&lt;/a&gt;),ID 为 aId。但是该元素在呈现的 HTML 中不包含所需的 id。像这样:

&lt;a href="" data-testid="aID"&gt;...&lt;/a&gt;

有什么想法吗?

【问题讨论】:

    标签: javascript testing ember.js handlebars.js dalekjs


    【解决方案1】:

    在 Ember 中,bind-attr 不应在您的 link-to 帮助内使用,因为它只能在 html 元素内使用:

    <a {{bind-attr href=myLink}}>My Link</a>
    

    在 Handlebars 助手中,您只需直接定义属性。

    {{#link-to "blub" data-testID="aID"}}{{loc "blub"}}{{/link-to}}
    

    如果缺少引号,则该属性不会呈现到 HTML 中。

    但是你还需要重新打开LinkView:

    Ember.LinkView.reopen({
        attributeBindings: ['data-testID']
    });
    

    See similar question here.

    And the Ember docs here.

    【讨论】:

    • 嘿,你成就了我的一天 :) 但我必须添加引号才能使其正常工作 --> @sheldonnbbaker 也谢谢你 :)
    【解决方案2】:

    尝试使用引号:

    {{#link-to "blub" bind-attr data-testid="aID"}}{{loc "blub"}}{{/link-to}}

    缺少引号将导致 Ember 尝试在当前视图上下文中查找属性,而不是按照您的意愿吐出一个字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-19
      • 2012-04-09
      • 1970-01-01
      • 1970-01-01
      • 2013-01-27
      • 1970-01-01
      相关资源
      最近更新 更多