将表单数据推送到firebase无法正常工作

时间:2016-08-20 19:15:02

标签: angularjs firebase angularjs-scope firebase-realtime-database

嗨我从角度推送数据到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中的任何内容

抱歉我的英语不好。英语不是我的主要语言 谢谢你的帮助

更新 我已经发现了我的问题 当我在mainCtrl中声明我的变量时

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

发送到firebase的这些东西和值不是来自输入表单(addrestaurant.html) 那么问题是我的控制器如何将输入表单中的值发送到firebase?

解决 最后我在这个链接中找到了解决方案https://stackoverflow.com/a/26905500/3628867

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我找到了以ng-model形式更改$ scope名称的解决方案 来自$ scope.namaResto = {nama:null};在控制器中 并以html格式表示ng-model =“namaResto.nama”

有关详细信息,请参阅此链接 https://stackoverflow.com/a/26905500/3628867