Javascript 如何清除angularJS数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29803045/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:01:34  来源:igfitidea点击:

How to clear an angularJS array

javascriptangularjs

提问by nullException

 $scope.itemarray = ['A', 'B', 'C'];  

this will clear the array but the ui wont be updated.

这将清除数组,但 ui 不会更新。

$scope.itemarray = [];

this works fine! why?

这很好用!为什么?

 $scope.itemarray.length = 0;  

回答by Petr Averyanov

$scope.itemarray.length = 0;<< this is correct. Length is read-write property.

$scope.itemarray.length = 0;<< 这是正确的。长度是读写属性。

$scope.itemarray = [];<< this creates new empty array. If you have bindings to old itemarray, they may be lost. (Html binding like ng-if="itemarray[0]"wont be lost)

$scope.itemarray = [];<< 这会创建新的空数组。如果您绑定到旧的 itemarray,它们可能会丢失。(Html 绑定之类的ng-if="itemarray[0]"不会丢失)