2017-05-22 15 views
1

매우 간단합니다. 그러나 나는 그것을 할 수 없다. 컨트롤러에서 내 Firebase DB의 가치를 얻고 싶습니다. 중포 기지 데이터베이스는 다음과 같이이다 : 반복이 잘 작동하는 HTML 코드의 ngRepeat에서Firebase 및 AngularJS, firebaseArray에서 값을 가져 오는 방법

app.controller('usersCtrl', function($scope, $firebaseArray) { 
    var ref = firebase.database().ref('users'); 
    $scope.userdata = $firebaseArray(ref); 
}); 

: 내 컨트롤러에서

users { 
    30549545 { 
     name: "Marcelo" 
     lastName: "Forclaz" 
     years: 24 
    } 
} 

내가 다음 코드를 썼다. 하지만 컨트롤러에서 "년"값을 다른 명령에 사용하려면 어떻게해야합니까? 어떻게 할 수 있습니까? 여러 가지 방법으로 시도했지만 원하는 결과를 얻지 못했습니다. Firebase width AngularJS에서 데이터를 가져 오는 것이 간단하고 순수한 JavaScript를 만드는 것보다 쉽지는 않다는 것을 깨달았습니다.

답변

1

마지막으로, 나는 그것을있다!

$scope.userdata.$ref().once('value', function(snap) { 
    angular.forEach(snap.val(), function(index)) { 
     console.log(index.years) 
    } 
} 
1

당신은 유저 데이터가로드 될 때까지 기다릴 필요가 :

app.controller('usersCtrl', function($scope, $firebaseArray) { 
var ref = firebase.database().ref('users'); 
$scope.userdata = $firebaseArray(ref); 
$scope.userdata.$loaded() 
    .then(function(){ 
     console.log($scope.userdata); 
    }); 
}); 
+0

그러나 이제 어떻게 "년"가치를 얻습니까? 난 당신의 코드를 작성하고 콘솔 로그는 참조 데이터와 함께 전체 개체를 반환합니다. 나는 단지 그의 가치가 필요하다. 나는 console.log ($ scope.data.years)를 시도했지만 정의되지 않은 값을 반환합니다. –