AngularFire 0.5美元移除.错误:Firebase.child失败:第一个参数是无效路径

AngularFire 0.5 $remove. Error: Firebase.child failed: First argument was an invalid path

本文关键字:第一个 失败 参数 路径 无效 child Firebase 美元 AngularFire 错误      更新时间:2023-09-26

这里是angular的新手,我有一个ng重复列表,带有ng click="remove(item)"按钮,可以触发删除功能:

$scope.remove = function (item) {
        console.log(item);
        $scope.items.$remove(item);
};

而$scope.items.$remove();可以很好地删除模型中的所有项。我不太清楚如果我传递了一个要删除的项目,为什么它会给我带来错误。下面是我得到的错误。

Error: Firebase.child failed: First argument was an invalid path: "[object Object]". Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"
    at Error (<anonymous>)
    at Ga (https://cdn.firebase.com/v0/firebase.js:12:230)
    at H.J.F (https://cdn.firebase.com/v0/firebase.js:134:213)
    at Object.object.$remove (https://cdn.firebase.com/libs/angularfire/0.5.0/angularfire.js:160:26)
    at Scope.MyCtrl.$scope.removeShape (http://127.0.0.1:9000/scripts/controllers/main.js:24:23)
    at http://127.0.0.1:9000/bower_components/angular/angular.js:9977:21
    at http://127.0.0.1:9000/bower_components/angular/angular.js:17678:17
    at Scope.$eval (http://127.0.0.1:9000/bower_components/angular/angular.js:11668:28)
    at Scope.$apply (http://127.0.0.1:9000/bower_components/angular/angular.js:11768:23)
    at Scope.$delegate.__proto__.$apply (<anonymous>:855:30) angular.js:9193
(anonymous function) angular.js:9193
(anonymous function) angular.js:6746
Scope.$apply angular.js:11770
$delegate.__proto__.$apply VM21684:855
(anonymous function) angular.js:17677
jQuery.event.dispatch jquery-1.9.1.js:3074
elemData.handle

提前感谢您帮助我了解发生了什么。

$remove将字符串作为参数,即要删除的子项的键名,而不是对象本身。在上遍历键和ng中的值repeat,这样您就可以访问该键。例如:

<div ng-repeat="(key, item) in items">
  <a ng-click="remove(key)">Remove</a>
</div>

您也可以在$index键中使用"build",该键可以在angular ng repeat中使用,并将$index传递给您的remove方法:

<div ng-repeat="item in items">
  <a ng-click="remove($index)">Remove</a>
</div>