简体   繁体   English

在Angular JS中覆盖$ digest和$ watch

[英]Override $digest and $watch in Angular JS

Can anyone suggest how do I create a service to over ride angular js default $watch and $digest loop? 谁能建议我如何创建一个服务来覆盖角度js默认的$ watch和$ digest循环?

My requirement is to $watch a scope object with a listener and the listener function should have 4 parameters, @param1 - newObject, @param2 - oldObject, @param3 - Exact attribute which changed, @param4 - the heirarchy of the attribute in that object to that attribute which changed. 我的要求是$ watch具有侦听器的范围对象,并且侦听器函数应具有4个参数,@ param1-newObject,@ param2-oldObject,@ param3-更改的确切属性,@ param4-该对象中属性的层次结构更改的属性。

required :- 必需的:-

$scope.$watch('objectName', function(param1,param2,param3,param4){
   //log param3 and param4 in a stack for later use. 
   //Note:- I dont want to log entire changed object since its too big.
}, true);

NOTE:- Right now we can deep watch an object but the listener would provide us with 2 objects, old object and new object. 注意:-现在我们可以深入观察一个对象,但是侦听器将为我们提供2个对象,旧对象和新对象。 All I want is to get the exact attribute changed. 我想要的只是更改确切的属性。

Any help will be highly appreciated. 任何帮助将不胜感激。 Please refer the below link for my complete problem. 请参阅以下链接了解我的完整问题。

Undo Redo In Angular JS 在Angular JS中撤消重做

I think you can probably create an undo system that uses some sort of classification system for various types of undo along with a more event oriented approach. 我认为您可以创建一个撤消系统,该撤消系统将某种分类系统用于各种类型的撤消,以及更加面向事件的方法。 I focused on comments you made about iterating whole objects for each keystroke so will start there 我专注于您针对每次击键迭代整个对象的评论,因此将从此处开始

I'm assuming that you aren't updating server for every keystroke on inputs in which case you wouldn't need to watch anything other than the specific input's key events while it is in focus. 我假设您不是针对输入的每次击键更新服务器,在这种情况下,您无需关注特定输入的键事件,就不必关注其他任何事情。 When you bind CTRL +Z to the document, do it in a way that nothing global happens when an input is in focus. 当您将CTRL + Z绑定到文档时,以一种使输入焦点对准全局不会发生的方式进行操作。

Following scenario is based on simplest UI action to track...update field only. 以下方案基于最简单的UI动作来仅跟踪...更新字段。

On focus of an input you would immediately take a copy of it's current model value, and store that as value to use for a complete undo of the ng-model later. 专注于输入时,您将立即获取其当前模型值的副本,并将其存储为值,以供日后完全撤消ng-model时使用。

While in focus, CTRL+Z would be bound to that input and keystrokes events can be undone at very local level not impacting any need to watch at higher level. 在重点关注时,将CTRL + Z绑定到该输入,并且可以在非常本地的级别撤消击键事件,而不会影响任何在更高级别观看的需求。 Unbind key events for that input on blur. 在模糊时取消绑定该输入的按键事件。

On blur of an input, you would then be able to $emit an update event that you would track as category like "field update". 输入模糊后,您便可以$emit一个update event ,该update event将作为“字段更新”之类的类别进行跟踪。 Send the old stored value , the object that contains the ng-model and update category to a tracking service that listens for updates and pushes updates into an undo array. 将包含ng-model和update类别的旧存储值(包含ng-model和update类别的对象)发送到跟踪服务,该服务监听更新并将更新推送到undo数组中。

When user now hits undo, service looks at update type and changes the value of the object associated to the field name stored and removes that last stored item from the queue 当用户现在单击undo时,服务将查看更新类型并更改与存储的字段名称关联的对象的值,并从队列中删除最后存储的项目

I did something similar to this using only jQuery for a UI that was set up to both create and manage a web page layout with absolute positioned elements throughout, as well as inserting and updating content. 我只使用了jQuery来完成与此类似的操作,该jQuery被设置为创建和管理整个页面中具有绝对定位元素的网页布局,以及插入和更新内容。

I've only started on user level input that assumes no new data objects created or deleted. 我仅在假定没有创建或删除新数据对象的用户级别输入上开始。 Those might be handled by $.watch watching array lengths. 那些可能由$ .watch监视数组长度来处理。 By only watching length you wont have to iterate over every property of every object contained to figure out what changed. 通过仅观察长度,您就不必遍历所包含的每个对象的每个属性来找出发生了什么变化。

One helpful tracking property for all of this is the $$hashKey property that angular gives to every object it touches in all scopes. 所有这些有用的跟踪属性是$$hashKey属性,该属性将angular赋予它在所有范围内接触的每个对象。 Using haskey could help map array length changes to find differences. 使用haskey可以帮助映射数组长度更改以找到差异。 I haven't though all of this section through a lot yet. 我还没有完成本节的所有内容。

Hope the event driven input tracking concept helps. 希望事件驱动的输入跟踪概念有所帮助。 At least at user input level it would cut out a significant amount of overhead using $watch 至少在用户输入级别,使用$ watch可以节省大量开销

Also angular recently added a shallower listener $watchCollection was added in 1.2. 另外, $watchCollection最近在1.2中添加了一个更浅的侦听器$watchCollection Haven't used it yet, but surely lightens overhead where it's use case might not warrant a full watch 尚未使用过,但在使用案例可能无法保证完整手表的情况下,无疑可以减少开销

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM