首页 > 解决方案 > 角度输入绑定性能

问题描述

对静态数据使用带括号和不带括号的输入绑定在性能上是否有任何差异?我是说

<my-component [customTitle]="'Hello World'"></my-component>

对比

<my-component customTitle="Hello World"></my-component>

如果有人分享一些与该主题相关的有用文章,我将不胜感激

标签: angulardata-binding

解决方案


如果使用静态数据,请使用属性装饰器

在构造函数中

constructor(@Attribute('customTitle') private customTitle: string) {}

如果您想在 html 中使用,请使用public而不是private,如果您计划组件可以使用或不使用@Optional

constructor(@Optional() @Attribute('customTitle') public customTitle: string) {
   //and you can use, e.g.
   if (!this.customTitle)
       this.title="a title by defect"
}

//or in your .html, e.g.
<div *ngIf="!customTitle">Title by defect</div>
{{customTitle}}

你可以检查一下Netanel Basal 的这个条目


推荐阅读