【问题标题】:Need to add a child with value in Firebase using AngularJS需要使用 AngularJS 在 Firebase 中添加一个具有价值的孩子
【发布时间】:2016-10-15 15:07:09
【问题描述】:

我需要使用 AngularJS 在我的 Firebase 中插入一个值为 0 的新子节点。

我尝试使用此命令插入但不起作用:

var ref = new Firebase(FBURL);
objectname = "name";
$scope.insert= ref.child("user/"+objectname);
$scope.insert.setvalue("Arthur");



result expected
firebaseID 
    |-------------user
                   |-----name:Arthur

【问题讨论】:

  • 您在问题中包含了 JSON 树的图片。请将其替换为实际的 JSON 作为文本,您可以通过单击 Firebase 数据库控制台中的导出按钮轻松获取该文本。将 JSON 作为文本使其可搜索,让我们可以轻松地使用它来测试您的实际数据并在我们的答案中使用它,一般来说这只是一件好事。

标签: angularjs firebase firebase-realtime-database


【解决方案1】:

你不是用 Angular 的方式来做的。 https://github.com/firebase/angularfire/blob/master/docs/guide/README.md


您可以使用三向数据绑定:

  var app = angular.module("sampleApp", ["firebase"]);

  // a factory to create a re-usable Profile object
  // we pass in a username and get back their synchronized data as an object
  app.factory("Profile", ["$firebaseObject",
    function($firebaseObject) {
      return function(username) {
        // create a reference to the database node where we will store our data
        var ref = firebase.database().ref("rooms").push();
        var profileRef = ref.child(username);

        // return it as a synchronized object
        return $firebaseObject(profileRef);
      }
    }
  ]);

  app.controller("ProfileCtrl", ["$scope", "Profile",
    function($scope, Profile) {

      //your object id
      var objectId = '-KSe_RHPYjhkjasjjh-o6';  
      // create a three-way binding to our Profile as $scope.profile
      Profile(objectId).$bindTo($scope, "profile");

      //now simple manipulate the $scope.profile variable
      $scope.profile.name = "Arthur";
    }
  ]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-30
    相关资源
    最近更新 更多