Firebase 3方式数据绑定无法按预期工作 [英] Firebase 3 way data binding not working as expected
问题描述
我有一个返回Firebase引用的AngularJS服务.
I have an AngularJS service that returns a firebase ref.
.factory('sessionManager', ['$firebase','$scope', function($firebase){
var ref=new Firebase('https://telechat.firebaseio.com/sessions');
return $firebase(ref);
}])
在控制器中,我添加了依赖项并称为$bind
.
In the controller, I have added the dependency and called $bind
.
$scope.items=sessionManager;
$scope.items.$bind($scope,'sessions').then(function(unbind){
unbind();
});
但是当我将其打印到控制台时,返回的数据除了数据数组之外,还具有诸如$add
,$set
等的功能的集合.
But when I print it to the console, the returned data has a collection of functions like $add
, $set
,.. etc in addition to the array of data.
为什么会这样?我做错了吗?
Why is this occurring? Am I doing it the wrong way?
推荐答案
如果我正确地阅读了该问题,您可能会觉得$ bind()是将$ firebase对象转换为原始数据数组的印象或对象.本质上,$ firebase实例和$ bind之间的唯一区别是数据是本地更改,它们是自动从Angular推回Firebase的.而当您使用不带$ bind的$ firebase时,则需要调用$ save来推送本地更改.
If I'm reading the question correctly, you may be under the impression that $bind() is a transformation of the $firebase object into a raw data array or object. In essence, the only difference between a $firebase instance and $bind is that data is local changes are automagically pushed back to Firebase from Angular. Whereas, when you use $firebase without $bind, you need to call $save to push local changes.
请记住,$ firebase是Firebase API的包装器,而不是简单的数据数组,在大多数情况下,您仍可以将其视为原始数据.
Keeping in mind that $firebase is a wrapper on the Firebase API and not a simple data array, you can still treat it like raw data in most cases.
要迭代Firebase对象中的数据,可以使用ngRepeat:
To iterate data in a Firebase object, you can use ngRepeat:
<li ng-repeat="(key, item) in $sessions">{{item|json}}</li>
或者如果您想应用依赖于数组的过滤器:
Or if you want to apply filters that depend on arrays:
<li ng-repeat="(key, item) in $sessions | orderByPriority | filter:searchText">
或者在使用$ getIndex的控制器中:
Or in a controller using $getIndex:
angular.forEach($scope.sessions.$getIndex(), function(key) {
console.log(key, $scope.sessions[key]);
});
已修改的$ add/$ update/etc方法是 $ firebase对象的API的一部分. 文档和
The $add/$update/etc methods mentoined are part of the $firebase object's API. The documentation and the tutorial should be great primers for understanding this process.
这也是API的一部分,该API正在不断发展以更好地匹配Angular的做事方式和用户的反馈.
This is also a part of the API that is continuing to evolve to better match the Angular way of doing things and feedback from users.
这篇关于Firebase 3方式数据绑定无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!