2016-08-20 42 views
0

嗨,我有問題,從角度將數據推送到firebase。我是新手在此,所以我需要幫助檢查什麼是錯我的代碼將表單數據推送到firebase不起作用

這是我的代碼: addrestaurant.html

<h4>Nama Restaurant</h4> 
    <input type="text" class="form-control" ng-model="namaResto" > 
    <h4>Alamat:</h4> 
    <input type="text" class="form-control" ng-model="alamatResto"> 
    <h4>Image Link: </h4> 
    <input type="text" class="form-control" ng-model="imgResto"> 
    <h4> Category Restoran </h4> 
    <input type="text" class="form-control" ng-model="categoryResto"> 
    <h4>Price range:</h4> 
    <input type="text" class="form-control" ng-model="pricerangeResto"> 
    <h4>Location:</h4> 
    <input type="text" class="form-control" ng-model="locationResto"> 

    <span class="input-group-btn"> 
     <button class="btn btn-default" type="button" ng-click="sendRestaurant()">Send</button> 
    </span> 

main.js

/*global Firebase*/ 
'use strict'; 

angular.module('firebaseApp') 
    .controller('MainCtrl', function ($scope, $timeout) { 
    var rootRef = new Firebase('https://yummy-364b1.firebaseio.com/'); 
    var childRest = rootRef.child('restaurant'); 
    var categoryRest =rootRef.child('category'); 

    $scope.namaResto = null; 
    $scope.alamatResto = null; 
    $scope.categoryResto = null; 
    $scope.imgResto = null; 
    $scope.pricerangeResto = null; 
    $scope.locationResto = null; 



    childRest.on('value', function(snapshot){ 
     $timeout(function(){ 

     var snapshotVal = snapshot.val(); 
     $scope.restaurant =snapshotVal; 
     }); 
    }); 

$scope.sendRestaurant = function() { 


     childRest.push().set({ 
     nama: $scope.namaResto, 
     alamat: $scope.alamatResto, 
     category: $scope.categoryResto, 
     img: $scope.imgResto, 
     pricerange: $scope.pricerangeResto, 
     locationarea: $scope.locationResto 
     }); 
    }; 


}); 

不知何故,當我刪除push()並只使用set()我現有的數據在firebase中被刪除。但如果我使用push()它不會改變任何在firebase

抱歉我的英語不好。英語不是我的主要語言 感謝您的幫助

UPDATE 我已經找到了我的問題 當我宣佈我的變量在mainCtrl

$scope.namaResto = null; 
$scope.alamatResto = null; 
$scope.categoryResto = null; 
$scope.imgResto = null; 
$scope.pricerangeResto = null; 
$scope.locationResto = null; 

這些東西和值發送到火力不能從輸入form(addrestaurant.html) 所以現在問題是我的控制器可以如何從我的輸入表單向firebase發送值?

解決 終於讓我找到解決辦法在這個環節https://stackoverflow.com/a/26905500/3628867

+0

我建議你先添加一個完成回調到你推來檢查任何可能出現的錯誤,並添加你的問題的結果。像這樣:childRest.push()。set(「我正在寫數據」,函數(錯誤)如果(錯誤)alert(「數據無法保存。」+錯誤); } else { 警報(「Data saved successfully。」); } }); –

+0

我厭倦了你的解決方案,它的工作原理當我推送字符串,但是當我將其更改爲變量$ scope.name它沒有工作,但對話框說數據保存成功 –

回答

0

試試這個:

$scope.sendRestaurant = function() { 
childRest.push({ 
    nama: $scope.namaResto, 
    alamat: $scope.alamatResto, 
    category: $scope.categoryResto, 
    img: $scope.imgResto, 
    pricerange: $scope.pricerangeResto, 
    locationarea: $scope.locationResto 
    }); 
}; 

來源:https://www.firebase.com/docs/web/guide/saving-data.html

+0

已經嘗試過,仍然沒有工作沒有任何事情發生在firebase –

0

我找到的解決方案,在NG-改變我的$範圍名模型形式 from $ scope.namaResto = {nama:null};在控制器 和HTML表單NG-模型=「namaResto.nama」

更多細節請參閱本鏈接 https://stackoverflow.com/a/26905500/3628867

相關問題