kennechu kennechu - 6 months ago 21
Javascript Question

Clear a list or dictionary in Angularjs with a key, value structure

A very basic question, I have a list or dictionary in AngularJS with a key,value structure like this:

$scope.itemList = {}


This list fills with data coming from a JSON file, what I need to do is to clear this list at some point but I dont know how to do it,

I tried the following but didnt worked.

$scope.itemList.clear()


Some help would be great.


Note: I said a list but actually is an object what I have because of
the {}, I'll leave the question as is, someone can think in a wrong
way as I did.

Answer

First thing is that declaring variable with {} means object not list.

To clear a object you have to re-define it again

$scope.itemList = {}

To declare a list you have to use like this

$scope.itemList = [];

And to clear list

Try like this

$scope.itemList.length=0;

or re-declare it again.

$scope.itemList = [];
Comments