【问题标题】:AngularFire 0.5 $remove. Error: Firebase.child failed: First argument was an invalid pathAngularFire 0.5 美元移除。错误:Firebase.child 失败:第一个参数是无效路径
【发布时间】:2013-12-16 07:58:47
【问题描述】:

这里是 Angular 的新手,我有一个 ng-repeat 列表,其中包含触发删除功能的 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

提前感谢您帮助我了解正在发生的事情。

【问题讨论】:

    标签: javascript angularjs angularfire


    【解决方案1】:

    $remove 将字符串作为参数,即要删除的子项的键名,而不是对象本身。迭代 both ng-repeat 中的键和值,以便您可以访问该键。例如:

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

    【讨论】:

    • 我明白了,它工作得很好而且很有意义。非常感谢!
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2017-09-12
      • 2017-12-01
      • 2015-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-15
      • 1970-01-01
      • 2020-12-21
      相关资源
      最近更新 更多