【问题标题】:Retriving item by id Firebase using AngularFire使用 AngularFire 通过 id Firebase 检索项目
【发布时间】:2015-11-15 21:22:23
【问题描述】:

我正在尝试搜索具有 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;
        }
    };
});

我一起努力:

        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);
};

【问题讨论】:

    标签: angularjs firebase angularfire


    【解决方案1】:

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

    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. 嵌套数组

    忽略这些考虑,今天开始可能会更容易,但您限制了应用程序的扩展范围。

    【讨论】:

    • 非常感谢,我不尝试混合数组和对象函数,再次感谢!
    猜你喜欢
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 2016-06-02
    • 2014-12-24
    相关资源
    最近更新 更多