【问题标题】:AngularJS directive - accessing parent attribute from templateUrl methodAngularJS 指令 - 从 templateUrl 方法访问父属性
【发布时间】:2023-03-25 22:39:01
【问题描述】:

我有一个带有嵌套项目的控件,比如项目列表。 让我们说:

列表

  • 项目 1
  • 项目 2
  • 第 n 项

在 item 指令中,我可以像这样设置模板 url:

templateUrl: (tElement, tAttrs) ->
    tAttrs.template or '/item-template.html'

它工作正常,但我想将模板 url 存储为父属性以避免为每个项目设置模板。显然每个项目的模板都是相同的。

目前尚未确定范围。所以我认为实现它的最简单方法:

templateUrl: (tElement, tAttrs) ->
    tElement.parent().attr('template') or '/item-template.html'

但我不确定这是最好的方法......

所以我的问题是 - 实现这一目标的最佳方法是什么?

谢谢!

【问题讨论】:

标签: javascript angularjs coffeescript


【解决方案1】:

我会用includes in jade. 解决这个问题

// index.jade
doctype html
html
  include includes/head
body
  h1 My Site
  p Welcome to my super lame site.
  include includes/foot

我喜欢将标记的东西放在代码之外,而你所做的事情会让人感觉标记。

【讨论】:

    猜你喜欢
    • 2012-08-08
    • 2015-06-05
    • 1970-01-01
    • 1970-01-01
    • 2017-06-18
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多