2015-08-21 34 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再次! –

相關問題