简体   繁体   English

如何从angularjs中删除摘要循环?

[英]how to remove digest cycle from angularjs?

I am familiar about the digest cycles in angularjs and how they work. 我对angularjs中的摘要循环及其工作方式非常熟悉。 my question is, can we remove digest cycle from angularjs and still be able to build angular app; 我的问题是,我们可以从angularjs中删除摘要循环,仍然能够构建angular应用程序吗? if yes how can we remove them. 如果是,我们如何删除它们。 I also want to know how can we delay the digest cycles. 我也想知道我们如何延迟摘要周期。

No, the digest cycle is the fundamental part of AngularJS that makes it work (two-way data binding and all that.) Why use AngularJS then if you want to break it into something that is no longer AngularJS? 不,摘要循环是使AngularJS正常工作的基础部分(双向数据绑定等等)。为什么要将AngularJS分解为不再是AngularJS的东西,为什么要使用AngularJS?

The digest cycle simply creates "watcher" abstractions to allow data to flow back and forth between the views and data layers. 摘要循环仅创建“观察者”抽象,以允许数据在视图和数据层之间来回流动。

If you don't want this, then I would suggest not using AngularJS, as you would be using bloated code for no benefit. 如果您不希望这样做,那么我建议您不要使用AngularJS,因为使用膨胀的代码毫无益处。

The short answer is already provided by other users, which is no. 简短答案已经由其他用户提供,否。

However we get to control when our data goes to view and when they don't. 但是,我们可以控制何时查看数据以及何时不查看数据。

For example an instant data-binding by default looks like this: 例如,默认情况下,即时数据绑定如下所示:

<input type='text' ng-model='data'>
{{data}}

What if we want to delay it, by say 0.5 seconds? 如果我们想延迟0.5秒怎么办? We can separate the view model and assign it the value when it is appropriate. 我们可以分离视图模型,并在适当时为其分配值。

<input type='text' ng-model='data'>
{{viewData}}

$scope.$watch(function(){
    $scope.data;
}, function(){
    $timeout(function(){
        $scope.viewData = angular.copy($scope.data);
    }, 500);
});

Or only display only when a button is clicked. 或仅在单击按钮时显示。

<input type='text' ng-model='data'>
<button ng-click='click()'>Click me!</button>
{{viewData}}

$scope.click = function() {
    $scope.viewData = angular.copy($scope.data);
}

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

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