2015-08-21 59 views
2

我正在尝试使用ID搜索项目,但收到此消息。所有功能都正常工作。当执行我得到这个错误:使用AngularFire重建项目id使用AngularFire的Firebase

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

我尝试TOGETHER:

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

回答

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

它的工作请参见本jsbin:http://jsbin.com/tumape/edit?html,js,output

但请阅读消息,你很仔细的链接页面,因为它指出了当前数据结构中的一些可伸缩性限制问题。在收藏

  1. 使用数组索引
  2. 嵌套阵列

通过忽略这样的考虑,很可能更容易今天就开始,但你限制你的应用将扩展的程度。

+0

Thx很多,我不尝试混合数组和对象函数,thx再次! –