2014-05-06 73 views
0

我有一個像嵌套項目列表項目的控制。 比方說:AngularJS指令 - 從templateUrl方法訪問父屬性

列表

  • 項目1
  • 項指令我可以設置模板URL像這裏面的第2項
  • 項n,

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

它工作在f但我想存儲模板URL作爲父母的屬性,以避免設置每個項目的模板。顯然,每個項目的模板都是一樣的。

範圍尚未設置在此階段。所以我覺得最簡單的方法來實現:

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

,但我不知道這是最好的辦法......

所以我的問題是 - 什麼是實現這一目標的最佳途徑?

謝謝!

+0

看看[在指令指南中創建通信的指令](https://docs.angularjs.org/guide/directive)。你可以做類似的事情,但你可以在(子)項指令中使require成爲可選項 – JoseM

回答

0

我與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 

我喜歡保持markupy東西出來的代碼,你在做什麼感覺markupy解決這個。