javascript - 如何清除 angularJS 数组

标签 javascript angularjs

 $scope.itemarray = ['A', 'B', 'C'];  

这将清除数组,但不会更新 ui。

$scope.itemarray = [];

这很好用!为什么?

 $scope.itemarray.length = 0;  

最佳答案

$scope.itemarray.length = 0; << 这是正确的。长度为读写属性。

$scope.itemarray = []; << 这会创建一个新的空数组。如果您绑定(bind)到旧的 itemarray,它们可能会丢失。 (像 ng-if="itemarray[0]" 这样的 Html 绑定(bind)不会丢失)

关于javascript - 如何清除 angularJS 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29803045/

相关文章:

javascript - 我的脚本被一些移动运营商内联 - 如何处理?

带有原型(prototype)的 JavaScript 继承—— 'constructor' 属性?

AngularJS服务更新 Controller 变量

javascript - 如何从angularjs中的路由构造 Controller 名称

javascript - 使用 angular.js 使用 Enter 键提交表单

javascript - 通过两个服务连接一个 promise

javascript - 带有推送事件数据的推送通知

javascript - 未定义的函数

javascript - Angular.js 页面转换 & $animate.addClass

javascript - AngularJS 将 Bootstrap 按钮组值传递给 Controller