使用 AngularFire 按 id Firebase 检索项目

Retriving item by id Firebase using AngularFire

我正在尝试搜索具有 ID 的项目,但我收到了这条消息。所有功能都正常工作。执行时出现此错误:

Storing data using array indices in Firebase can result in unexpected behavior. See https://www.firebase.com/docs/web/guide/understanding-data.html#section-arrays-in-firebase for more information.

我的工厂

.factory('TEST', function ($firebaseArray) {

    var ref = new Firebase('https://shark-firebase.firebaseio.com');

    return {
        all: function(section) {
            var data = $firebaseArray(ref.child(section));
            return data;
        },
        get: function(section, id) {
            var data = $firebaseArray(ref.child(section).child(id));
            return data;
        }
    };
});

我一起尝试:

        get: function(section, id) {
            var data = $firebaseArray(ref.child(section));
            return data.$getRecord(id);
        }

和我的控制器:

以防万一,$stateParams.section = 'posts' 和 $stateParams.id = '0'。

$scope.loadItem = function(){
    $scope.item = TEST.get($stateParams.section, $stateParams.id);
};

这是获得该物品的一种方法:

var app = angular.module('app', ['firebase']);

app.factory('TEST', function ($firebaseArray, $firebaseObject) {

    var ref = new Firebase('https://shark-firebase.firebaseio.com');

    return {
        all: function(section) {
            var data = $firebaseArray(ref.child(section));
            return data;
        },
        get: function(section, id) {
            var data = $firebaseObject(ref.child(section).child(id));
            return data;
        }
    };
});

app.controller('MainCtrl', function($scope, TEST) {
    $scope.item = TEST.get('posts', 0);
});

查看此 jsbin 是否有效:http://jsbin.com/tumape/edit?html,js,output

但是请仔细阅读您链接的消息中的页面,因为它指出了您当前数据结构中的一些可扩展性限制问题。

  1. 在集合上使用数组索引
  2. 嵌套数组

如果忽略这些注意事项,今天开始可能会更容易,但您会限制应用程序的扩展范围。