2016-08-14 70 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