AngularJS:为什么 ng-bind 在 angular 方面比 {{}} 更好? [英] AngularJS : Why ng-bind is better than {{}} in angular?

查看:22
本文介绍了AngularJS:为什么 ng-bind 在 angular 方面比 {{}} 更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在其中一个角度演示和会议中的一个人提到 ng-bind{{}} 绑定更好.

其中一个原因,ng-bind 将变量放在观察列表中,只有在模型更改时才会推送数据查看,另一方面,{{}} 每次都会插入表达式(我猜是角循环)并推送值,即使值改变与否.

另外据说,如果屏幕上没有太多数据,您可以使用{{}},并且性能问题将不可见.有人可以帮我解释一下这个问题吗?

解决方案

如果您没有使用 ng-bind,请改为:

你好,{{user.name}}

在解析 user.name 之前(在加载数据之前),您可能会看到实际的 Hello, {{user.name}} 一秒钟

你可以这样做

你好,<span ng-bind="user.name"></span>

如果这对您来说是个问题.

另一种解决方案是使用 ng-cloak.

I was in one of the angular presentation and one of the person in the meeting mentioned ng-bind is better than {{}} binding.

One of the reason, ng-bind put the variable in the watch list and only when there is a model change the data get pushed to view, on the other hand, {{}} will interpolate the expression every time (I guess it is the angular cycle) and push the value, even if the value changed or not.

Also it is said that, if you have not much data in on the screen you can use {{}} and the performance issue will not be visible. Can someone shed some light on this issue for me?

解决方案

If you are not using ng-bind, instead something like this:

<div>
  Hello, {{user.name}}
</div>

you might see the actual Hello, {{user.name}} for a second before user.name is resolved (before the data is loaded)

You could do something like this

<div>
  Hello, <span ng-bind="user.name"></span>
</div>

if that's an issue for you.

Another solution is to use ng-cloak.

这篇关于AngularJS:为什么 ng-bind 在 angular 方面比 {{}} 更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆