Sankar
Sankar

Reputation: 1292

How to Remove a array elements in angularJS like jQuery RemoveAll()?

I have a problem when loading a call. Before that I need to clear the array. I am using angularJS.

 $scope.Load = function ()
 {
    $scope.StudentDetails = [];
    $http({ method: 'GET', url: '/Home/GetStudentDetails' }).success(function (data)
    {
        if (data != null)
        {
            $.each(data.Data, function (index, value)
            {
                $scope.StudentDetails.push(value);
            });

           // $scope.checked = false;
        }
    }).error(function ()
    {
        alert("Failed");
    });
}

Here $scope.StudentDetails is my array every time I load the data. Data will append, not clearing the data before load.

Upvotes: 0

Views: 4350

Answers (2)

sunderls
sunderls

Reputation: 783

actually , you can clear a array by splice

var a = [1,2,3]
a.splice(0,3) // now a is an empty array

or simply a.splice(0) will work.

if you did it with a = [], it will actually create a new empty array, and the angular's two-way bindings will fail for some scenarios, since it's suddenly not the same array.

Upvotes: 2

vaibhav silar
vaibhav silar

Reputation: 3085

Clear array before pushing values. I think this code will solve your problem:

$scope.Load = function ()
 {
    $scope.StudentDetails = [];
    $http({ method: 'GET', url: '/Home/GetStudentDetails' }).success(function (data)
    {
        if (data != null)
        {
            $scope.StudentDetails = [];
            $.each(data.Data, function (index, value)
            {
                $scope.StudentDetails.push(value);
            });

           // $scope.checked = false;
        }
    }).error(function ()
    {
        alert("Failed");
    });
}

Upvotes: 0

Related Questions