Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear an angularJS array

 $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;   
like image 803
nullException Avatar asked Apr 22 '15 16:04

nullException


People also ask

Is empty array JS?

To check if an array is empty or not, you can use the . length property. The length property sets or returns the number of elements in an array. By knowing the number of elements in the array, you can tell if it is empty or not.


1 Answers

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

like image 87
Petr Averyanov Avatar answered Oct 03 '22 10:10

Petr Averyanov