2015-06-09 56 views
0

這是我關於堆棧溢出的第一個問題,所以請耐心等待:-)。

我有一個角度應用程序,我想顯示用戶信息,從firebase中檢索。

有趣的是,當我在指令內而不是控制器內進行Firebase請求時,我得到的firebase數據有點不同。從我的指令中檢索的firebase數據是一個包含方法的對象。從我的控制器中檢索的數據是純粹的json,只有我插入的數據。

指令

angular.module('routerangular') 
.directive('raEditor',[ "$firebaseArray", "$firebaseAuth", function($firebaseArray, $firebaseAuth){ 
return { 
    restrict: 'E', 
    replace: true, 
    controller: ["$scope", function ($scope) { 
    var messagesRef = new Firebase("https://***.firebaseio.com/messages"); 

    messagesRef.onAuth(authDataCallback); 

    function authDataCallback(authData) { 
     if (authData) { 
     $scope.users = $firebaseArray(messagesRef); 

     } 
    } 
    }] 
}; 
}]); 

我想在HTML中鍵入{{用戶}}訪問數據,但它只是說不確定。以下是console.log的輸出,我輸出從firebase收到的數據。

[] ***** this is the data I need *****0: Object$id: "-JrIUxOGb54jTV1R9zjy"$priority: nulldescription: "awesome chattionado"userName: "mcChat".....**** To try and keep this short, I have deleted some of the methods from the console.log, not data ****..... $save:() { [native code] }$watch:() { [native code] }length: 1__proto__: Array[0] 

控制器:

在控制器中,我使用的幾乎相同的代碼,但我得到一個適當的JSON數據從火力,其中,I可以通過鍵入{{用戶檢索數據集}}在html中。

angular.module('routerangular') 
.controller('DisplayvideoCtrl', function ($scope, $firebaseObject,  $firebaseArray, $firebaseAuth) { 


    var messagesRef = new Firebase("https://****.firebaseio.com/messages"); 

    messagesRef.onAuth(authDataCallback); 
    function authDataCallback(authData) { 
     if (authData) { 
     $scope.users = $firebaseArray(messagesRef); 
     } 
    } 
}); 

控制器只是輸出存儲在JSON數據:

[{"description":"awesome chattionado","userName":"mcChat"}] 

缺少什麼我在這裏?爲什麼我無法從指令中檢索Firebase中的數據?

+0

你在哪裏輸入'{{users}}'在你的html中?在你的指令模板中? – AWolf

+0

你可以添加你的html標記嗎?我在你的js中看不到任何問題。 – AWolf

+0

這是我的html,我把這個指令和一個測試{{user}}放在一起。

{{用戶}}

martin

回答

0

$ scope在指令中的表現略有不同,隔離範圍僅繼承顯式聲明的屬性。請查看#10前十錯誤Scoping $scope's

由於{{users}}從您的指令中返回一個對象,請嘗試{{users.description}}和{{users.userName}}。

+0

我試圖{{users.description}},但它只是返回未定義。我的指令「console.log($ scope.users)」中的console.log顯示了這一點。 [] 0:對象 $$加入:(){[本地代碼]} $$錯誤:(){[本地代碼]} $$信息getKey:(){[本地代碼]} 而在「0」內是我的數據。但我根本找不到他們。我試過: $ scope.users.0 $ scope.users [「0」] $ scope.users.description 等 但沒有任何運氣。 謝謝大家的反饋。 – martin

0

謝謝大家的回覆,我部分地想通了,並認爲我可以解決它。解決方法是,指令中的對象帶有兩個變量:$ id和$ value,它們可以用於ng-repeat中以生成firebase中的鍵/值。謝謝你們。

相關問題