Solved

Retrieve data from firebase with promise in AngularJS/Ionic Application

Posted on 2016-08-23
1
200 Views
Last Modified: 2016-08-24
Hi experts,

I have a Ionic Application where I try to include Firebase. I got it work so far but the way I included the get request in my controller my script just continue without waiting for the data. I would like to convert my request into a request with a promise.

something similar as I used before with normal $http requests:

 
$http(http_req).then(function mySucces(data) {
// Success
}, function myError(data) {
//something was wrong
});

Open in new window


My current controller looks like this:

.controller('NoticiasCtrl', function(FURL, $scope, $state, $http, $ionicLoading, $rootScope, $firebaseObject, $firebaseArray) {
	
    $scope.newslist = [];

    $scope.doRefresh = function() {
  
        //Show loading comment
    	$ionicLoading.show({
        template: '<p>Carregando notícias...</p><ion-spinner></ion-spinner>'
    	});

        var ref = firebase.database().ref('news');
        $scope.newslist = $firebaseArray(ref);

        $ionicLoading.hide();
        $scope.$broadcast('scroll.refreshComplete');

    	};

	$scope.doRefresh();

})

Open in new window


Basically I want to have the line "var ref = firebase...." with a promise. And if the data is received continue with $scope....

Thank you in advance
0
Comment
Question by:Oliver2000
1 Comment
 

Accepted Solution

by:
Oliver2000 earned 0 total points
ID: 41769144
I figured it out myself

firebase.database().ref('news')
  .then ( function (ref) {
        $scope.newslist = $firebaseArray(ref);
        $ionicLoading.hide();
        $scope.$broadcast('scroll.refreshComplete')
  });

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Ransomware is a growing menace to anyone using a computer or mobile device. Here are answers to some common questions about this vicious new form of malware.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question