簡體   English   中英

如何在模板中訪問Angular 1.5組件屬性

[英]How to access Angular 1.5 component property inside a template

我試圖在Angular 1.5中實現一個簡單的組件我試圖訪問組件mycomponentheader'中的綁定(綁定)屬性'compTitle'。

var mycomponentheader = {
    ....
    bindings: {
    'comptitle': '@mycomptitle'
    },
    ....
};

我在視圖中的html標記中傳遞屬性值[compTitle =“encryptedTitle”]。

  <mycomponentheader mycomptitle="encryptedTitle">
    <div>    
      This is displayed for component content
    </div>
  </mycomponentheader>

我嘗試在指令中使用類似的機制。 相同的jsfiddle是@ https://jsfiddle.net/visibleinvisibly/4n7vsas7/

我知道將模板屬性定義為可以在Angular 1.5中使用$ element和$ attrs(template:function($ element,$ attrs){}注入的函數,但我正在尋找其他方法。

提前致謝

看一下組件指南

你會看到,默認情況下,綁定綁定到controller ,它們在$ctrl ,而不是this ,因為使用了controllerAs

comptitle ,只需從$ctrl訪問comptitle ,如: $ctrl.comptitle

這是工作演示。

注意:在組件指南中,您將看到一個列表,其中包含組件和指令之間的主要差異。 您會注意到某些mycomponentheader屬性不需要,因為它們不存在於組件中。 這些屬性包括: restrict (已經默認為E )和replace (不支持)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM