2016-11-12 97 views
0

我試圖從Firebase數據庫檢索我的待辦任務,但沒有數據顯示在我的視圖中。我的控制檯沒有錯誤。將數據firebase檢索到列表

我到達「將數據保存在firebase中」。

我的代碼:

var app = angular.module('starter', ['ionic', 'firebase']); 
 

 
var fb = null; 
 

 
app.run(function($ionicPlatform) { 
 
    $ionicPlatform.ready(function() { 
 
    if(window.cordova && window.cordova.plugins.Keyboard) { 
 
     cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); 
 
     cordova.plugins.Keyboard.disableScroll(true); 
 
    } 
 
    if(window.StatusBar) { 
 
     StatusBar.styleDefault(); 
 
    } 
 

 
    var config = { 
 
     apiKey: "...", 
 
     authDomain: "...", 
 
     databaseURL: "...", 
 
     storageBucket: "...", 
 
     messagingSenderId: "..." 
 

 
    firebase.initializeApp(config); 
 

 
    }); 
 
}); 
 
    
 
app.controller('TodoCtrl', function($firebaseObject,$scope, $ionicPopup){ 
 
    $scope.list = function(){ 
 

 
    var user = firebase.auth().currentUser; 
 

 
    if(user != null){ 
 
     alert('user ' + user.uid + ' exists!'); 
 

 
     var ref = firebase.database().ref('/users/').child(user.uid); 
 
     var syncObject = $firebaseObject(ref); 
 
     syncObject.$bindTo($scope, "data"); 
 

 
    }else{ 
 
     alert('error'); 
 
    } 
 
    } 
 

 

 
    $scope.create = function() { 
 
    var user = firebase.auth().currentUser; 
 

 
    $ionicPopup.prompt({ 
 
     title: 'Enter a new TODO item', 
 
     inputType: 'text' 
 
    }) 
 
    .then(function(result) { 
 
     if(result !== "") { 
 
     firebase.database().ref("https://stackoverflow.com/users/").child(user.uid).push({ 
 
      todos: result 
 
     }); 
 

 
     } 
 
    }); 
 
    } 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<ion-view view-title="Todo" ng-init="list()"> 
 
    <ion-nav-buttons side="right"> 
 
     <button class="right button-icon icon ion-plus" ng-click="create()" ></button> 
 
    </ion-nav-buttons> 
 
    <ion-content> 
 
     <div class="list"> 
 
     <div ng-repeat="todo in data.todos" class="item"> 
 
      {{todo.todos}} 
 
     </div> 
 
    </ion-content> 
 
</ion-view>

My Firebase database

在此先感謝您的幫助。

回答

0

爲什麼不在聲明後執行列表函數並讓事情正常工作。

var app = angular.module('starter', ['ionic', 'firebase']); 
 

 
var fb = null; 
 

 
app.run(function($ionicPlatform) { 
 
    $ionicPlatform.ready(function() { 
 
    if(window.cordova && window.cordova.plugins.Keyboard) { 
 
     cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); 
 
     cordova.plugins.Keyboard.disableScroll(true); 
 
    } 
 
    if(window.StatusBar) { 
 
     StatusBar.styleDefault(); 
 
    } 
 

 
    var config = { 
 
     apiKey: "...", 
 
     authDomain: "...", 
 
     databaseURL: "...", 
 
     storageBucket: "...", 
 
     messagingSenderId: "..." 
 

 
    firebase.initializeApp(config); 
 

 
    }); 
 
}); 
 
    
 
app.controller('TodoCtrl', function($firebaseObject,$scope, $ionicPopup){ 
 
    var list = function(){ 
 

 
    var user = firebase.auth().currentUser; 
 

 
    if(user != null){ 
 
     alert('user ' + user.uid + ' exists!'); 
 

 
     var ref = firebase.database().ref('/users/').child(user.uid); 
 
     var syncObject = $firebaseObject(ref); 
 
     syncObject.$bindTo($scope, "data"); 
 

 
    }else{ 
 
     alert('error'); 
 
    } 
 
    } 
 

 
    // execute the function at this point. 
 
    list(); 
 

 
    $scope.create = function() { 
 
    var user = firebase.auth().currentUser; 
 

 
    $ionicPopup.prompt({ 
 
     title: 'Enter a new TODO item', 
 
     inputType: 'text' 
 
    }) 
 
    .then(function(result) { 
 
     if(result !== "") { 
 
     firebase.database().ref("https://stackoverflow.com/users/").child(user.uid).push({ 
 
      todos: result 
 
     }); 
 

 
     } 
 
    }); 
 
    } 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<!-- you are fine to make minor changes here--> 
 
<ion-view view-title="Todo" ng-controller="TodoCtrl"> 
 
    <ion-nav-buttons side="right"> 
 
     <button class="right button-icon icon ion-plus" ng-click="create()" ></button> 
 
    </ion-nav-buttons> 
 
    <ion-content> 
 
     <div class="list"> 
 
     <div ng-repeat="todo in data.todos" class="item"> 
 
      {{todo.todos}} 
 
     </div> 
 
    </ion-content> 
 
</ion-view>

+0

維克多嗨!我正在嘗試改變它們,但它們不起作用... –

+0

Up!你幫不了我? –

+0

你能解決這個問題的地方? –