gpt4 book ai didi

javascript - 如何像 jQuery RemoveAll() 一样删除 angularJS 中的数组元素?

转载 作者:行者123 更新时间:2023-12-02 17:46:53 25 4
gpt4 key购买 nike

我在加载通话时遇到问题。在此之前我需要清除数组。我正在使用 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是我每次加载数据时的数组。数据将追加,加载前不会清除数据。

最佳答案

其实,你可以通过拼接来清除数组

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

或者简单地使用a.splice(0)即可。

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

关于javascript - 如何像 jQuery RemoveAll() 一样删除 angularJS 中的数组元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21669600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com