如何从angular.js中的范围“删除”变量[复制]

时间:2022-04-30 11:26:46

This question already has an answer here:

这个问题在这里已有答案:

According to my knowledge when we attach a variable to the scope , watches are applied to it and it is checked every digest cycle. A good rule of thumb is that we should not have more than 2000 variables being watched at a given time.

根据我的知识,当我们将一个变量附加到范围时,会对其应用监视并在每个摘要周期检查它。一个好的经验法则是,我们不应该在给定时间内监视超过2000个变量。

My question is how do you remove an already present variable from scope. For example $scope.var1=1 say I had to create it for a one time use. Is it possible for me to "delete" it from the scope or will the variable be watched for the life time of the scope ?

我的问题是如何从范围中删除已存在的变量。例如$ scope.var1 = 1说我必须创建它一次性使用。我是否可以从示波器中“删除”它,或者在示波器的使用寿命期间监视变量?

EDIT :

编辑:

From the comments below I understand that you are supposed to remove the watches manually or they get destroyed when the scope gets destroyed. However I am still unclear as to how will you remove watches for variables which are set by directives such as ngModel ?

从下面的评论中我了解到你应该手动移除手表,否则当范围被破坏时它们会被销毁。但是我仍然不清楚如何删除由ngModel等指令设置的变量的监视?

1 个解决方案

#1


33  

You can simply use the delete keyword:

您只需使用delete关键字:

delete $scope.var1;

#1


33  

You can simply use the delete keyword:

您只需使用delete关键字:

delete $scope.var1;