2016-08-14 88 views
1

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

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/

我所知定義模板屬性作爲一個函數,它可以與$元件和$ ATTRS(模板被注入的:在角1.5功能($元件,$ ATTRS){} )但我正在尋找其他方法。

由於提前

回答

0

看一看的component's guide

你會看到,默認情況下,綁定綁定到controller,他們是$ctrl下,而不是this,由於使用controllerAs

這就是說,只需從$ctrl訪問comptitle,就像這樣:$ctrl.comptitle

Here is the working demo.

注:對組件的指南,你會看到組件和指令之間的主要差異列表。您會注意到一些mycomponentheader屬性不是必需的,因爲它們不存在於組件中。這些屬性包括:restrict(它已默認爲E)和replace(不受支持)。

+0

謝謝你的工作 – visibleinvisibly

相關問題