使用AngularFire通过ID Firebase重新搜索项目

Mar*_*sco 2 angularjs firebase angularfire

我正在尝试搜索带有id的项目,但是我收到了此消息.所有功能都正常工作.执行时我收到此错误:

使用Firebase中的数组索引存储数据可能会导致意外行为.有关 详细信息,请参阅 https://www.firebase.com/docs/web/guide/understanding-data.html#section-arrays-in-firebase.

我的工厂

.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;
        }
    };
});
Run Code Online (Sandbox Code Playgroud)

我一起试试:

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

和我的控制器:

如果是,$ stateParams.section ='posts'和$ stateParams.id ='0'.

$scope.loadItem = function(){
    $scope.item = TEST.get($stateParams.section, $stateParams.id);
};
Run Code Online (Sandbox Code Playgroud)

Fra*_*len 5

以下是获取该项目的一种方法:

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);
});
Run Code Online (Sandbox Code Playgroud)

看到这个jsbin工作:http://jsbin.com/tumape/edit?html,js,output

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

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

忽略这些考虑因素,今天开始可能更容易,但是你限制了应用程序扩展的程度.