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
How to clear an angularJS array
提问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]"不会丢失)

