Angular2,为什么每次我移动鼠标时都会调用函数 [英] Angular2, why functions are called each time I move the mouse

查看:106
本文介绍了Angular2,为什么每次我移动鼠标时都会调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Angular2种子( https://github.com/mgechev/angular2-seed ),而我遇到了一个问题.

I use Angular2 seed (https://github.com/mgechev/angular2-seed), and I encounter a problem.

实际上,在我的模板html中,我使用了一些绑定,例如{{test}}和在组件中带有变量test的绑定,以及使用{{getTest()}}的绑定,该绑定调用了getTest()函数.

Indeed, in my template html, I use some bindings like {{test}} with a variable test in the component, and {{getTest()}} which calls the getTest() function.

如果我在getTest()中使用控制台日志,那么每次移动鼠标时,都会得到一个日志.

If I use a console log in getTest(), each time I move the mouse, I get a log.

这是理想的行为吗?

我一直认为只有在值更改时它才会更新,就像观察者一样. 我是否必须使用观察者来避免每次调用此调用?

I was thinking it will update only if the value changes, like with an observer. Did I have to use an observer to avoid this call each time ?

谢谢.

推荐答案

Angular2更改检测检查绑定是否在每次运行时都更新,并且通常在激发监听事件时才运行.

Angular2 change detection checks if bindings are updated every time it is run and it is usually run when events that are listened to are fired.

这就是为什么不鼓励在绑定中使用方法调用的原因.

This is why it is discouraged to use method calls in bindings.

这篇关于Angular2,为什么每次我移动鼠标时都会调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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