How to clear an angularJS array

JavascriptAngularjs

Javascript Problem Overview


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

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

$scope.itemarray = [];

this works fine! why?

 $scope.itemarray.length = 0;  

Javascript Solutions


Solution 1 - Javascript

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

$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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnullExceptionView Question on Stackoverflow
Solution 1 - JavascriptPetr AveryanovView Answer on Stackoverflow