如何“停止监视”一位前pression [英] How to 'unwatch' an expression

查看:141
本文介绍了如何“停止监视”一位前pression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个NG重复一个大阵。

Say I have an ng-repeat with a big array.

当纳克重复运行时,它增加了该阵列的分离的范围的每一个元素,以及具有在一个范围数组本身。这意味着,$摘要检查更改整个阵列,并且最重要的是,它会检查的每一个个体元素的数组的变化

When ng-repeat runs, it adds every element of that array to an isolated scope, as well as having the array itself in a scope. That means that $digest checks the entire array for changes, and on top of that, it checks every individual element in that array for changes.

请参阅这plunker 因为什么我谈论的例子。

See this plunker as an example of what I'm talking about.

在我的使用情况下,我不会改变我的数组的单个元素,所以我没有必要让他们看着你。我将只改变整个阵列,在这种情况下纳克重复将重新呈现表中它的全部内容。 (如果我错了,这个请让我知道。)

In my use case, I never change a single element of my array so I don't need to have them watched. I will only ever change the entire array, in which case ng-repeat would re-render the table in it's entirety. (If I'm wrong about this please let me know..)

在(比如说)1000行的数组,那就是我不需要评估1000多前pressions。

In an array of (say) 1000 rows, that's 1000 more expressions that I don't need evaluated.

我怎样才能注销从监视器的每个元素,同时还观看主阵列?

也许不是注销我可以有我的$消化的更多的控制权,并以某种方式跳过各个行?

Perhaps instead of deregistering I could have more control of my $digest and somehow skip each individual row?

此特定情况下实际上是更普遍的问题的一个例子。我知道, $表返回一个'deregisteration功能,但并不能帮助时,指令注册的手表,这是大部分时间

This specific case is actually an example of a more general issue. I know that $watch returns a 'deregisteration' function, but that doesn't help when a directive is registering the watches, which is most of the time.

推荐答案

为了有一个大阵,你不看看每个项目的中继器。

您需要创建一个自定义指令,它有一个参数,和前pression到您的数组,然后在链接功能,你只是看的数组,你必须在链接功能编程刷新在HTML(而不是使用NG重复)

You'll need to create a custom directive that takes one argument, and expression to your array, then in the linking function you'd just watch that array, and you'd have the linking function programmatically refresh the HTML (rather than using an ng-repeat)

像(psuedo- code):

something like (psuedo-code):

app.directive('leanRepeat', function() {
    return {
        restrict: 'E',
        scope: {
           'data' : '='
        },
        link: function(scope, elem, attr) {
           scope.$watch('data', function(value) {
              elem.empty(); //assuming jquery here.
              angular.forEach(scope.data, function(d) {
                  //write it however you're going to write it out here.
                  elem.append('<div>' + d + '</div>');
              });
           });
        }
    };
});

...这似乎像一个痛苦的对接。

... which seems like a pain in the butt.

备用hackish的方法

您也许可以遍历 $范围。$$观察家并检查 $范围。$$观察家[0] .exp.exp ,看它是否符合你想删除前pression,然后用一个简单的拼接()通话将其删除。这里的PITA,是东西像胡说{{无论}}胡说标记之间将是前pression,甚至包括回车。

You might be able to loop through $scope.$$watchers and examine $scope.$$watchers[0].exp.exp to see if it matches the expression you'd like to remove, then remove it with a simple splice() call. The PITA here, is that things like Blah {{whatever}} Blah between tags will be the expression, and will even include carriage returns.

在从正面看,你可能能够只是遍历你的NG-重复的$范围,只是删除一切,然后明确地添加你想要的手表......我不知道。

On the upside, you might be able to just loop through the $scope of your ng-repeat and just remove everything, then explicitly add the watch you want... I dunno.

无论哪种方式,它似乎是一个黑客。

Either way, it seems like a hack.

删除由$范围做了一个守望者。$看

您可以注销 $观看 $观看调用返回的功能:

You can unregister a $watch with the function returned by the $watch call:

例如,有一个 $观看只火一次:

For example, to have a $watch only fire once:

var unregister = $scope.$watch('whatever', function(){ 
     alert('once!');
     unregister();
});

您当然可以调用函数注销任何时候你想......这只是一个例子。

You can, of course call the unregister function any time you want... that was just an example.

结论:是不是真的有一个伟大的方式做你问什么

但有一点要考虑:是它甚至值得担忧?再者就是它确实有成千上万的记录加载到几十DOMElements每一个好主意吗?回味无穷。

But one thing to consider: Is it even worth worrying about? Furthermore is it truly a good idea to have thousands of records loaded into dozens of DOMElements each? Food for thought.

我希望帮助。

编辑2(去除坏主意)

这篇关于如何“停止监视”一位前pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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