2015-03-08 55 views
0

我想檢索對象的數據和結果顯示在控制檯上,但它只顯示頁面上的一個結果(最後的結果)。Firebase檢索數據只顯示一個結果

$http.get('/users').success(function(data) { 
     $scope.username = data; //assign data to username 
     console.log(data); 
}); 

而且在我玉模板,我有

section(data-ng-controller='browse') 
h4 hi 
ul(data-ng-repeat='user in username') 
li {{user}} 

我想遍歷這個數據

{ 
    "google: xxxxxxxxxxxxx" : { 
    "name" : "ZZZ YYY", 
    "profile" : { 
     "briefProfile" : "Cost call", 
     "career" : "Web Developer", 
     "projects" : { 
     "-JjtSgiwkqFxTxMv0Gip" : "http://zipper.com" 
     } 
    }, 
    "provider" : "google" 
    }, 
    "google:xxxxxxxxxxx" : { 
    "name" : "SSS TTT", 
    "profile" : { 
     "briefProfile" : "Desired Intelligence", 
     "career" : "Consultant" 
    }, 
    "provider" : "google" 
    } 
} 

謝謝。(我不使用angularFire)。

+0

請更新您的問題開始。就目前而言,由於您使用'$ http'來請求您的數據,因此它與Firebase無關。 – 2015-03-09 00:51:44

回答

1

如果您使用$http服務,那麼您並未使用Firebase。

  1. 您正在重複ul而不是li。此外,{{user}}將成爲整個用戶對象,並且您將在頁面上看到類似[Object object]的內容,而不是字符串。所以請致電{{user.name}}

    ul 
    li(data-ng-repeat='user in users') {{user.name}} 
    
  2. 假設$http有誤,並且你想用火力地堡相反,你可能希望通過查看火力地堡Getting Started guide

    // Get a reference to users 
    var ref = new Firebase('https://<YOUR-FIREBASE>.firebaseio.com/users'); 
    
    // Attach an asynchronous callback to read the data at users ref 
    ref.on("value", function(snapshot) { 
        console.log(snapshot.val()); 
        $scope.users = snapshot.val(); 
    }, function (errorObject) { 
        console.log("The read failed: " + errorObject.code); 
    }); 
    
+0

OMG,我現在覺得很愚蠢。這很傷心,我從來沒有意識到這一點。錯誤是'ul和li'。非常感謝你@Sinan和@Elias。我真的很感激。 – 2015-03-09 07:55:39

+0

我想我忘了關注前端代碼 – 2015-03-09 08:11:47

+0

不是問題!前端代碼有時候是一個棘手的問題。我也建議檢查'ng-bind'指令。它比「{{...}}更好」 - https://docs.angularjs.org/api/ng/directive/ngBind – 2015-03-09 15:40:58

1

如果您使用AngularFire你應該在這個看一看:

而不是使用$ http.get嘗試:

var data = $firebaseArray(new Firebase("URL")); 
$scope.data = data; 

不要忘了加$ firebaseArray作爲依賴於你的控制器。在AngularFire文檔

更多信息: https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebasearray

如果你不使用AngularFire看看這個:

var firebaseRef = new Firebase('YOUR-FIREBASE-URL/users/'); 
firebaseRef.on('value', function(dataSnapshot) { 
    var data = dataSnapshot.val(); 
    console.log(data); 
    $scope.users = data; 

    // Might need to use $digest to update $scope. 
    $scope.$digest(); 
}); 

更多關於()的火力地堡文檔: https://www.firebase.com/docs/web/api/query/on.html

+0

對不起,我沒有使用angularFire :) – 2015-03-08 18:13:16

+0

嗨@BabajideFowotade我已經更新了上面的答案,看看下面的「如果你不使用AngularFire看看這個:」希望幫助! – Elias 2015-03-08 18:39:18

+0

您好@Elias我已經嘗試了您所問,但遺憾:(它仍然無法正常工作,它仍然會返回最後的數據,但控制檯上的完整數據 – 2015-03-08 20:44:06