它会影响性能的角度有范围的一切吗? [英] Does it affect Angular performance to have everything in scope?

查看:156
本文介绍了它会影响性能的角度有范围的一切吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如果任何人有$范围的使用建议VS普通的JavaScript对象时,一个模块内。比如我有一个控制器一些变数,我连接到​​有关我的便利范围,但他们可能只是控制器内一个有规则的,没有任何功能上的差异。

我的问题是它会影响性能时,角进入消化循环有范围的一切吗?

是这样的:

  $ scope.viewpanel = {};
$ scope.viewpanel.date =新的日期();
$ scope.viewpanel.day = $ scope.viewpanel.date.format('D');
$ scope.viewpanel.week = $ scope.viewpanel.date.format('W');
$ scope.viewpanel.month = $ scope.viewpanel.date.format('M');
$ scope.viewpanel.year = $ scope.viewpanel.date.format(O);

比这更好或更差:

  VAR的viewPanel = {};
viewpanel.date =新的日期();
viewpanel.day = viewpanel.date.format('D');
viewpanel.week = viewpanel.date.format('W');
viewpanel.month = viewpanel.date.format('M');
viewpanel.year = viewpanel.date.format('O');


解决方案

是的,但它如果你在你的模板,它自动创建观察家使用它会影响性能。如果他们不绑定到任何东西,那么它不管那么多了(有一些开销增加),但很多人不喜欢拥挤的范围。 这对斯科普斯wiki文章将解释当你添加一些$范围

另一种方法是结合一次或一次角的。这就像普通约束力,但只设置它一次,因此减少了观察家的数量。

此外,为您具体的例子,你可以考虑使用过滤器来格式化日期。虽然这不会与性能本身的帮助,它会给你清洁code。

I was wondering if anyone has advice for the usage of $scope vs plain JavaScript objects when inside a module. For instance I have some variables in a controller that I am attaching to $scope for my convenience but they could just be a regular object inside the controller without any functional difference.

My question is does it affect performance when Angular enters a digest cycle to have everything in scope?

Is this :

$scope.viewpanel = {};
$scope.viewpanel.date = new Date();
$scope.viewpanel.day = $scope.viewpanel.date.format('d');
$scope.viewpanel.week = $scope.viewpanel.date.format('W');
$scope.viewpanel.month = $scope.viewpanel.date.format('m');
$scope.viewpanel.year = $scope.viewpanel.date.format('o');

better or worse than this :

var viewpanel ={};
viewpanel.date = new Date();
viewpanel.day = viewpanel.date.format('d');
viewpanel.week = viewpanel.date.format('W');
viewpanel.month = viewpanel.date.format('m');
viewpanel.year = viewpanel.date.format('o');

解决方案

Yes, it does affect performance if you are using it in your template, which creates watchers automatically. If they aren't bound to anything, then it doesn't matter that much (there is some overhead added), but lots of people don't like crowded scopes. This wiki article on Scopes will explain exactly what happens when you add something to $scope.

Another alternative is bind-once or angular-once. It's like regular binding but only sets it one time so it reduces the number of watchers.

Also, for your specific example, you may consider using filters to format dates. Although that won't help with performance per se, it'll give you cleaner code.

这篇关于它会影响性能的角度有范围的一切吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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