如何使用Angularjs修改Firebase中元素的值

时间:2017-01-13 16:27:14

标签: angularjs firebase firebase-realtime-database

以下是我的Firebase数据结构:

See Example Here

如何访问/修改数据库中状态子项的值? 我试过这样的东西,但它不起作用......

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

app.controller("todoctrl", ["$scope", "$firebaseArray",
  function($scope, $firebaseArray) {
    var list = $firebaseArray(new Firebase('https://todo-5a386.firebaseio.com/'));
    var status = list.child("status");
    // set status
    $scope.set() = function(){
        status.$set("$scope.status")
    };

  }
]);

1 个答案:

答案 0 :(得分:0)

我相信这是您正在寻找的,以便能够在DOM中查看和修改“状态”:

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

app.controller("todoctrl", ["$scope", "$firebaseObject",
  function($scope, $firebaseObject) {
    var ref = new Firebase('https://todo-5a386.firebaseio.com/').child("status");

    //this will make status available to modify and usable in the DOM in ng-model or the like
    $scope.status = $firebaseObject(ref);

    $scope.status.$loaded().then(function(){
        //data is now loaded from firebase
        console.log("status: " + $scope.status);
    });

  }
]);

然后可以在DOM中进行访问或修改,例如:

<label>Firebase Status</label>
<input type="text" ng-model="status"/>

修改

documentation进行3路绑定:

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

app.controller("todoctrl", ["$scope", "$firebaseObject",
  function($scope, $firebaseObject) {
    var ref = new Firebase('https://todo-5a386.firebaseio.com/').child("status");

    //this will make status available to modify and usable in the DOM in ng-model or the like
    var obj = $firebaseObject(ref);

    obj.$loaded().then(function(){
        //data is now loaded from firebase
        console.log("status: " + obj);
    });


    $scope.status = obj;
    obj.$bindTo($scope, "status");
  }
]);

这应该是3方式绑定,因此您对状态所做的更改应反映在数据库中。