Solved

Retrieve data from firebase with promise in AngularJS/Ionic Application

Posted on 2016-08-23
1
93 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

New! My Passport Wireless Pro Wi-Fi Mobile Storage

Portable wireless storage to offload, edit, and stream anywhere.

High-capacity, wireless mobile storage designed to accompany professional photographers and videographers in the field to easily offload, edit and stream captured photos and high-definition videos.

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
With mobile becoming increasingly important to the business world, it is in your best interest to make sure that your email signature looks great across all types of devices. Find out how to create a mobile email signature with this guide.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now