插值{{}}和[innerText]之间的性能差异 - 角度2

Gra*_*les 4 performance angularjs angular

在Angular 1中使用ng-bind可以提高插值性能.

Angular 2中的情况仍然如此吗?我应该使用[innerText]过插值.

例如

<p>{{slower}}</p>
<p [innerText]="faster"></p>
Run Code Online (Sandbox Code Playgroud)

Avn*_*kya 8

我们经常在插值和属性绑定之间进行选择.以下绑定对执行相同的操作:

Interpolated: <img src="{{vehicle.imageUrl}}"><br>
Property bound: <img [src]="vehicle.imageUrl">

The interpolated title is {{title}}

[innerHTML]="'The [innerHTML] title is '+title">
Run Code Online (Sandbox Code Playgroud)

在许多情况下,插值是属性绑定的便利替代方案.事实上,Angular会在渲染视图之前将这些插值转换为相应的属性绑定.

没有技术理由偏爱一种形式到另一种形式.我们倾向于可读性,这往往有利于插值.我们建议建立编码风格规则,并选择既符合规则又对手头的任务感觉最自然的形式.

资料来源:https://angular.io/docs/ts/latest/guide/template-syntax.html#!#property-binding

所以你可以使用任何人.希望这会帮助你.

  • 但是有一个主要的区别.`{{}}`是插值,这意味着值将首先被字符串化.因此,如果要传递非字符串(如对象或数组),请不要使用`{{}}`. (2认同)