在角/微风初始化程序KO.Computed相当于 [英] KO.Computed equivalent in Angular / Breeze Initializer

查看:127
本文介绍了在角/微风初始化程序KO.Computed相当于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图让治疗数据如何绑定角度和理解的更好,一方面更深入的了解,很难左右让我的头 -

Trying to get a more in-depth understanding of how Angular treats data binding and understanding it better and one thing is difficult to get my head around -

在淘汰赛我用计算机来跟踪变化的一个属性。在角是这个逻辑迁入的观点,这是微不足道的我,但如果是做我理解的方式。

In Knockout I use a computed to keep track of changes to a property. In Angular it to move this logic into the view, which is a it trivial to me, but if that is the way to do it I understand.

我的问题是,当我用初始化微风一个新的实体/角如何创建发生更改时的实体属性都会得到通知计算样性质?

My question is when I am initializing a new entity with Breeze/Angular how do I create computed-like properties that are notified when changes occur to the entities property?

myEntity.fullName = ko.computed(function () {
    return myEntity.firstName + ' ' + myEntity.LastName;
});

在角就相当于是

myEntity.fullName = function () {
    return myEntity.firstName + ' ' + myEntity.LastName;
};

和这是否恰当地跟踪实体?

And does that properly track the entity?

推荐答案

您是否正确简单地使它的功能。如果如图所示添加到 $范围您的实体,那么你可以访问属性,像这样:

You are correct to simply make it a function. If your entity as shown is added to the $scope, then you would access the property like so:

<span class="fullname">{{ user.fullName() }}</span>

每当角运行 $消化周期,它会检查更改了绑定属性。在这种情况下,这意味着它将调用全名()功能检查,如果结果发生了变化。如果有,任何有 $观看附加到项目 - 包括简单的结合 - 将更改的通知

Whenever Angular runs a $digest cycle, it will check for a change to the bound property. In this instance, it means it will call the fullName() function and check to see if the result has changed. If it has, anything that has a $watch attached to that item — including simple binding — will be notified of the change.

该技术的一个警告,但是,是要确保你正在函数中执行的操作是比较快的,而且没有任何副作用。像这样绑定功能将整个应用程序调用多次。

One caveat of this technique, however, is to make sure that the operations being performed within your function are relatively fast, and also have no side effects. Bound functions like this will be called many times throughout the application.

如果您需要有一个更复杂的功能,这将是更好地处理控制器内,并在对象上更新属性手动更改时。

If you need to have a more complex function, it would be better to handle that within the controller, and update a property on the object manually when it changes.

这篇关于在角/微风初始化程序KO.Computed相当于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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