【发布时间】:2013-12-15 17:37:04
【问题描述】:
很抱歉这篇文章太长了,但这让我有点抓狂:
假设我想在加载时获取项目的“标题”和“.priority”键值。
首先让我们看看每个东西是如何布置的:
$scope.items = $firebase(new Firebase("https://****.firebaseio.com"));
$scope.items.$on('loaded', function() {
console.log($scope.items);
});
我们得到:
> Object {$bind: function, $add: function, $save: function, $set: function, $remove: function…}
> $add: function (item, cb) {
> $bind: function (scope, name) {
> $child: function (key) {
> $getIndex: function () {
> $on: function (type, callback) {
> $remove: function (key) {
> $save: function (key) {
> $set: function (newValue) {
> this is my item: Object
$$hashKey: "012"
$id: "this is my item"
$priority: 2884707
title: "this is the title of my item"
__proto__: Object
> __proto__: Object
看起来不错,让我们尝试获取 $priority:
console.log($scope.items.$child('$priority'));
哎呀:
Uncaught Error: Firebase.child failed: First argument was an invalid path: "$priority". Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"
好的,我们现在先跳过它,试试标题:
console.log($scope.items.$child('title'));
> Object {$bind: function, $add: function, $save: function, $set: function, $remove: function…}
> $add: function (item, cb) {
> $bind: function (scope, name) {
> $child: function (key) {
> $getIndex: function () {
> $on: function (type, callback) {
> $remove: function (key) {
> $save: function (key) {
> $set: function (newValue) {
> __proto__: Object
没有标题。
好的,让我们换一种方式试试吧:
var firstItem = $scope.items.$getIndex()[0]; //returns $ids in order of priority
console.log($scope.items.$child(firstItem));
> Object {$bind: function, $add: function, $save: function, $set: function, $remove: function…}
> $add: function (item, cb) {
> $bind: function (scope, name) {
> $child: function (key) {
> $getIndex: function () {
> $on: function (type, callback) {
> $remove: function (key) {
> $save: function (key) {
> $set: function (newValue) {
title: "this is the title of my item"
> __proto__: Object
但是现在没有优先级了!
厨房水槽对着电脑发誓:
console.log($scope.items.title);
> undefined
console.log($scope.items[0].title);
> Uncaught TypeError: Cannot read property 'title' of undefined
console.log(Object.keys($scope.items));
> ["$bind", "$add", "$save", "$set", "$remove", "$child", "$on", "$getIndex"]
我错过了什么?我可以通过掉头来获得“标题”,(使用“$getIndex()”找出项目的 $id 是什么,抓住第 n 个项目,然后使用“$child()”进行另一个调用),但即使这样也不适用于 $priority。有没有更简单的方法从 AngularFire v2 中获取关键值?或者有什么方法可以获得 $priority?
【问题讨论】:
-
这里有两件事。第一个是 $child 会收到一个记录的键,所以你想尝试像
$scope.items.$child('record1').$priority这样的东西。其次,请尝试升级到latest/official 0.5 release,因为 Anant 刚刚做了一些可能适用于此的修复。如果没有帮助,请报告您的发现。 -
这是我要链接的版本,'$scope.items.$child('record1').$priority' 给了我'undefined',这是正确的,因为没有返回 $priority当使用 'console.log($scope.items.$child('record1'))' 时,如我的“好吧,让我们以不同的方式尝试”示例。如果应该这样,那可能是一个错误。
-
我不清楚。关键是 $child 的操作就像 Firebase.child()。 angularFire 转到 Firebase,在当前路径上调用 .child(),并为该子路径返回一个新的 $firebase 对象。因此,您不能要求 $child('$priority') 因为这不是有效的 Firebase 密钥。这不是从对象获取子键的方法,而是对 Firebase 路径的新同步引用。因此,将使用
$scope.items['$priority']或类似的方式获得子密钥(我在上面写错了——对不起)。 -
对不起,如果我误解了,但
console.log( $scope.items.$child('the_id').$child('title'))工作正常,但console.log( $scope.items.$child('the_id')['$priority'])返回“未定义”,console.log($scope.items['$priority'])也是如此。这是意料之中的,因为console.log( $scope.items.$child('the_id'))不会像console.log( $scope.items)对每个对象那样返回$$hashkey、$priority 或$id。这让我想知道这是否是一个错误。 -
很可能是一个错误,很难说,因为我还没有完全理解。但是 $child('$priority') 永远不会起作用,因为 Firebase 路径键中不允许使用
$。这是解决此问题的第一步,即准确了解 $child 的作用,即返回一个全新的 $firebase 引用(不仅仅是数据中给定键的值)
标签: firebase angularfire