繁体   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