如何在对象上调用 angularfire 的 $bindTo()
后从 firebase 中删除对象。出于某种原因,调用 $bindTo()
似乎从对象中删除了 $remove()
函数。
例如,除非您注释掉 $bindTo
行,否则以下应用中的删除按钮不起作用。
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/