AlCode
AlCode

Reputation: 565

AngularJS showing error on console, when destroy() called

So i use destroy() to delete items from my $scope.array on click. It works fine, when i open my console the following is showing:

$scope.array[index].destroy is not a function

so i delete this line of code, in hope it will fix the console error, and it did but after that my app is not working. I checked if it is something else it is NOT it's just this, it is working but the error is still showing. I have no idea maybe because it's an older version?

EDIT: Whole function that is called on ng-click:

    $scope.SpliceItems = function (index)
    {
            $scope.array.splice(index, 1);
        }
   $scope.DestroyItem = function(index) {

            $scope.array[index].destroy();
        }

    $scope.remove = function(item)
    {
     for (var i = 0; i < $scope.array.length; i++) {
                if ($scope.array[i].Id == item.Id) {     
                   $scope.SpliceItems(i);
                   $scope.DestroyItem(i);

        }
    }

EDIT 2

So, whats the problem here? I need to dynamically multiply with this function, when adding or removing from array:

$scope.getTotal= function() {
        var total = 1;
        for (var i = 0; i < $scope.array.length; i++) {
            total *= $scope.array[i];
        }
        if (total == 1)
            total = 0;
        return total;
    }

When i use JUST SPLICE it doesn't change the value of total, it ALWAYS multiplies. When i use splice + destory() it is working fine, but the browser console gives me error for some reason.

Upvotes: 0

Views: 120

Answers (2)

User2
User2

Reputation: 1293

If you want to remove element from an array try this,

$scope.array = ['a', 'b']; 
$scope.array.splice(index,1); 

Upvotes: 0

Bananan
Bananan

Reputation: 609

Use splice() instead of destroy() e.g.: $scope.array.splice(index, 1);

Upvotes: 2

Related Questions