【问题标题】:How to modify value of element in Firebase with Angularjs如何使用 Angularjs 修改 Firebase 中元素的值
【发布时间】:2017-01-13 16:27:13
【问题描述】:

这是我的 Firebase 数据结构:

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

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")
    };

  }
]);

【问题讨论】:

  • 不太清楚您要完成什么。你能解释更多吗?您是否只是尝试访问状态属性或修改它?
  • 我想修改status属性。实际上,我想将所有状态值更改为现在设置为 ng-model="status" 的值。
  • 请详细说明“不起作用”。您没有在代码中显示您实际在哪里做任何事情,但乍一看 $firebaseArray 似乎不适合数据样本。这会将数据库的全部内容同步到类似数组的结构中。

标签: angularjs firebase firebase-realtime-database


【解决方案1】:

我相信这是您正在寻找的,以便能够查看和修改 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");
  }
]);

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 2020-06-09
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-13
    相关资源
    最近更新 更多