0

I would like my application to have an area of the screen that tracks activity.

I created the following:

$scope.activity = [];

What I am thinking of is that when something starts I will push onto this array like this:

$scope.activity.push("Loading content 1");
$scope.activity.push("Loading content 2");
$scope.activity.push("Loading content 3");
$scope.activity.push("Loading content 4");

I can then have an area of my screen that shows what is happening with an ng-repeat that shows everything in the array:

<div ng-repeat="row in activity">
    {{ row }}
<div>

My problem is I am not sure how to remove the items from the array once the activities have completed. Can someone give me a suggestion as to how I could do this. What I really need is some kind of pull function where I can specify the name of what I pushed and have it removed. Something like:

 $scope.activity.pull("Loading content 4");

Also I need another function such as:

 $scope.activity.update("Loading content 4", status);

I'm looking for a solution that does not use jQuery, or underscore. Myusers are IE9 and above.

Alan2
  • 19,668
  • 67
  • 204
  • 365
  • possible duplicate: http://stackoverflow.com/questions/3954438/remove-item-from-array-by-value – Philip G Dec 01 '13 at 08:47
  • See if this helps https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf – elclanrs Dec 01 '13 at 08:48

3 Answers3

2

Use Array splice method:

Changes the content of an array, adding new elements while removing old elements.

CD..
  • 65,131
  • 24
  • 138
  • 151
  • I'm sorry I just added something else to the question. I hope I can use your suggestion also for updating. – Alan2 Dec 01 '13 at 08:50
  • 1
    This is what you need https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype#Methods. – elclanrs Dec 01 '13 at 08:52
1

You could do this:

var activityArray = [];
activityArray.push("Loading content 1");
activityArray.push("Loading content 2");
activityArray.push("Loading content 3");
activityArray.push("Loading content 4");

//find the item we want to delete
var index = activityArray.indexOf('Loading content 4');// returns 3
activityArray.splice(index,1)//remove the item at index 3
Villarrealized
  • 807
  • 1
  • 6
  • 13
1

.pop() and .shift() are often used along with .push().

sbking
  • 7,440
  • 21
  • 32