【问题标题】:AngularFire single objectAngularFire 单个对象
【发布时间】:2013-04-22 13:58:18
【问题描述】:

在 angularFire 示例中,它展示了如何从 firebase 获取对象集合。

app.controller('ctrl', ['$scope', '$timeout', 'angularFireCollection',
  function($scope, $timeout, angularFireCollection) {    
    var url = 'https://ex.firebaseio.com/stuff';
    $scope.col = angularFireCollection(url);
  }
]);

只有一个对象呢?

我尝试过这样的事情:

  fb.child('stuff/'+id).on('value', function(snapshot) {
    $scope.obj = snapshot.val();
    console.log('hey got the value')
    console.log(snapshot.val())
  });

似乎不起作用。控制台正确输出对象值,但控制器没有更新。

【问题讨论】:

标签: javascript angularjs firebase angularfire


【解决方案1】:

尝试使用常规的 angularFire 服务,并指定单个对象的类型:

app.controller('ctrl', ['$scope', '$timeout', 'angularFire',
  function($scope, $timeout, angularFire) {    
    var url = 'https://ex.firebaseio.com/stuff';
    angularFire(url, $scope, "obj", "");
  }
]);

注意第四个参数(“”表示字符串,也可以使用布尔值、数字、对象和数组)。

【讨论】:

  • 我收到Error: type mismatch
  • URL 上的单个对象是字符串吗?您必须将第 4 个参数更改为与“ex.firebaseio.com/stuff”或您计划存储的对象类型相同的对象。还要确保使用最新版本的库:firebase.github.io/angularFire/angularFire.js
  • 这是一个对象,所以在我用括号替换引号后它就起作用了。不过,这个 API 对我来说毫无意义,我最初尝试将“对象”作为最后一个参数。有这方面的一些文件吗?您如何看待标准化/允许单个对象和集合使用相同的 api,这样我们就不必加载多个提供程序(希望使用正确的术语)?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-26
  • 2017-06-03
相关资源
最近更新 更多