javascript - 如何在调用 $bindTo 后删除 angularfire 对象

标签 javascript angularjs firebase angularfire

如何在对象上调用 angularfire 的 $bindTo() 后从 firebase 中删除对象。出于某种原因,调用 $bindTo() 似乎从对象中删除了 $remove() 函数。

例如,除非您注释掉 $bindTo 行,否则以下应用中的删除按钮不起作用。

Codepen

JS

var app = angular.module('myApp', ['firebase']);

app.controller('myCtrl', MyCtrl);

function MyCtrl($scope, $firebaseObject) {
  var self = this;
  this.test = $firebaseObject(new Firebase('https://bindtoissue.firebaseio-demo.com/test'));

  //if you comment out this line then the delete button will work.
  this.test.$bindTo($scope, 'ctrl.test');

  this.test.$loaded(function() {
    self.test.one = 1;
    self.test.two = 2;
    self.test.three = 3;
  });
}

MyCtrl.prototype.delete = function() {
  //$remove is undefined
  this.test.$remove();
}

HTML

<div ng-app="myApp" ng-controller="myCtrl as ctrl">
    <button ng-click="ctrl.delete()">delete</button>
    <pre>{{ctrl.test | json}}</pre>
</div>

最佳答案

请注意,$bindTo() 不会将同步对象放在作用域中,只是将数据放在作用域中。在这种情况下,您已将同步的 $firebaseObject 放在 this.test 中,然后还将数据绑定(bind)到 this.test

请注意,在此示例中没有理由使用 $bindTo()。您可以在不创建三向绑定(bind)的情况下调用 $remove(),这仅在您不想手动调用 $save() 时对对象进行本地更改时有用。

此外,这里 $loaded 的用法不正确。当您使用 $bindTo() 时,您会想要使用它返回的 promise 。

this.test.$bindTo($scope, 'ctrl.test')
  .then(function() {
    self.test.one = 1;
    self.test.two = 2;
    self.test.three = 3;
  });

关于javascript - 如何在调用 $bindTo 后删除 angularfire 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29933225/

相关文章:

javascript - 验证数组内属性对象的值

html - 使用 ngClass 时 CSS 样式不适用

javascript - 以 react 形式将 firebase.firestore.timestamp 转换为数据的 patchValue 之前的日期

javascript - React.js 验证

javascript - 相对表单操作解析为绝对 URL?

javascript - Mapbox map 已初始化

java - 由 : java. lang.RuntimeException : Android classes not found error. 引起 是什么原因导致的?

android - Firebase Cloud Firestore 在 asia-south1(孟买)不可用?

当输入空数据时,Javascript表单验证仍然提交表单

javascript - 如何在es6中替换 `bind(this)`