Firebase 3 路数据绑定无法按预期工作 [英] Firebase 3 way data binding not working as expected

查看:24
本文介绍了Firebase 3 路数据绑定无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回 firebase ref 的 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 的一部分,以更好地匹配 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆