對不起,這有點讓我有點瘋狂:使用AngularFire v2獲取關鍵值有點清晰嗎?
假設我想在加載時獲得一個項目的「title」和「.priority」鍵值。
首先,讓我們看到的每一件事情是如何佈局:
$scope.items = $firebase(new Firebase("https://****.firebaseio.com"));
$scope.items.$on('loaded', function() {
console.log($scope.items);
});
隨着我們得到:
> Object {$bind: function, $add: function, $save: function, $set: function, $remove: function…}
> $add: function (item, cb) {
> $bind: function (scope, name) {
> $child: function (key) {
> $getIndex: function() {
> $on: function (type, callback) {
> $remove: function (key) {
> $save: function (key) {
> $set: function (newValue) {
> this is my item: Object
$$hashKey: "012"
$id: "this is my item"
$priority: 2884707
title: "this is the title of my item"
__proto__: Object
> __proto__: Object
看起來不錯,讓我們試着搶$優先級:
console.log($scope.items.$child('$priority'));
糟糕:
Uncaught Error: Firebase.child failed: First argument was an invalid path: "$priority". Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"
好,我們將跳過,就目前而言,只是嘗試標題:
console.log($scope.items.$child('title'));
> Object {$bind: function, $add: function, $save: function, $set: function, $remove: function…}
> $add: function (item, cb) {
> $bind: function (scope, name) {
> $child: function (key) {
> $getIndex: function() {
> $on: function (type, callback) {
> $remove: function (key) {
> $save: function (key) {
> $set: function (newValue) {
> __proto__: Object
沒有標題。
好吧,讓我們嘗試用不同的方式:
var firstItem = $scope.items.$getIndex()[0]; //returns $ids in order of priority
console.log($scope.items.$child(firstItem));
> Object {$bind: function, $add: function, $save: function, $set: function, $remove: function…}
> $add: function (item, cb) {
> $bind: function (scope, name) {
> $child: function (key) {
> $getIndex: function() {
> $on: function (type, callback) {
> $remove: function (key) {
> $save: function (key) {
> $set: function (newValue) {
title: "this is the title of my item"
> __proto__: Object
但是現在有沒有優先級!在電腦前的時間
廚房水槽宣誓:
console.log($scope.items.title);
> undefined
console.log($scope.items[0].title);
> Uncaught TypeError: Cannot read property 'title' of undefined
console.log(Object.keys($scope.items));
> ["$bind", "$add", "$save", "$set", "$remove", "$child", "$on", "$getIndex"]
我缺少什麼?我可以設法通過做兩次U形轉彎來獲得'標題'(弄清楚該項目的$ id使用'$ getIndex()',抓住第n個項目,然後使用'$ child()'進行另一個調用),但即使如此也不適用於優先權。有沒有更簡單的方法從AngularFire v2中獲取關鍵值?或以任何方式獲得優先權?
這裏有兩件事。首先是$ child會得到一個記錄的關鍵字,所以你想嘗試一下'$ scope.items。$ child('record1')。$ priority'。其次,請嘗試升級到[最新/官方0.5版本](https://cdn.firebase.com/libs/angularfire/0.5.0/angularfire.js),因爲Anant只是做了一些適用於這裏的修復。如果這沒有幫助,請回報您的發現。 – Kato
這是我連接到的版本和'$ scope.items。$ child('record1')。$ priority'給我'undefined',這是正確的,因爲當'console.log($ scope.items。使用$ child('record1'))',就像在我的「好吧,讓我們嘗試一種不同的方式」的例子。如果它應該那麼這可能是一個錯誤。 – jthomasbailey
我還不清楚。問題的關鍵是,$子如同Firebase.child()一樣運行。 angularFire轉到Firebase,在當前路徑上調用.child(),併爲該子路徑返回一個新的$ firebase對象。因此,您不能要求$ child('$ priority'),因爲這不是有效的Firebase密鑰。這不是從對象獲取子鍵的方式,而是一種新的對Firebase路徑的同步引用。因此子鍵可以通過'$ scope.items ['$ priority']'或類似的方式獲得(我在上面寫錯了 - 對不起)。 – Kato