Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何删除angularJS中的数组元素,如jQuery RemoveAll()?_Javascript_Angularjs - Fatal编程技术网

Javascript 如何删除angularJS中的数组元素,如jQuery RemoveAll()?

Javascript 如何删除angularJS中的数组元素,如jQuery RemoveAll()?,javascript,angularjs,Javascript,Angularjs,我在加载呼叫时遇到问题。在此之前,我需要清除阵列。我正在使用angularJS $scope.Load = function () { $scope.StudentDetails = []; $http({ method: 'GET', url: '/Home/GetStudentDetails' }).success(function (data) { if (data != null) { $.each(da

我在加载呼叫时遇到问题。在此之前,我需要清除阵列。我正在使用
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");
    });
}

这里
$scope.StudentDetails
是每次加载数据时的数组。数据将追加,而不是在加载前清除数据。

在推送值之前清除数组。我认为此代码将解决您的问题:

$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");
    });
}

实际上,可以通过拼接来清除阵列

var a = [1,2,3]
a.splice(0,3) // now a is an empty array
或者干脆
a.拼接(0)
就可以了


如果您使用
a=[]
,它实际上会创建一个新的空数组,并且angular的双向绑定在某些情况下会失败,因为它突然不是同一个数组。

您是否尝试过使用angular.forEach而不是$.each?两个代码都是相同的。我也在使用此代码。但无法得到确切的结果。@Sankar,`$scope.StudentDetails=[]`@Satpal Sir是否有从数组中删除数据的函数,如jQuery array.removeAll()@Sankar,Try
$scope.StudentDetails.length=0先生,这很有效。但我需要精确的解决方案。如果你有想法,请与我分享,先生。。