Javascript 如何从 angular.js 的作用域中“删除”一个变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32103590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to "delete" a variable from scope in angular.js
提问by Kiran Yallabandi
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.
根据我的知识,当我们将变量附加到 scope 时,会对其应用监视,并在每个摘要周期检查它。一个好的经验法则是,在给定时间我们不应有超过 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 等指令设置的变量的监视?
回答by aghidini
You can simply use the delete
keyword:
您可以简单地使用delete
关键字:
delete $scope.var1;