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

时间:2015-04-29 02:20:00

标签: javascript angularjs firebase angularfire

如何在对象上调用angularfire' s #!/bin/bash list1=("I" "You" "We") list2=("want to eat" "want to buy" "want to steal") list3=("a banana" "a hamburger" "an icecream") for i in "${list1[@]}"; do for j in "${list2[@]}"; do for k in "${list3[@]}"; do echo $i" "$j" "$k done done done 后从firebase中删除对象。出于某种原因,调用$bindTo()似乎从对象中删除了$bindTo()函数。

例如,除非您注释掉$remove()行,否则以下应用中的删除按钮无效。

Codepen

JS

$bindTo

HTML

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();
}

1 个答案:

答案 0 :(得分:1)

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

请注意,在此示例中,没有理由使用$ bindTo()。您可以在不创建3向绑定的情况下调用$ 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;
  });