简体   繁体   中英

how do i retrieve and update my firebase data in angular

var myapp = angular.module('point',['firebase']);

myapp.factory('fact', function ($firebaseArray)
{
    var ref = new Firebase("https://myangularl.firebaseio.com");
    return $firebaseArray(ref);
})



myapp.controller('financeCtrl', function ($scope,fact)
{
    $scope.students = fact;

    $scope.add = function (data)
    {



            var name = $scope.name;
            var major = $scope.major;

            $scope.students.$add({
                name: name, major: major
            });

        $scope.name="";
        $scope.major="";

    },

    $scope.remove = function(data)
    {
        $scope.students.$remove(data);
    }
});

i am new to angular and firebase and i am having trouble getting my data from firebase so my user can edit and update. My remove and add functions are working fine, but i cannot seem to understand how the concept of updating. any help would be appreciated.

$firebaseArray automatically keeps local array in sync with any changes made to the remote database. To make change to to remote database, you can use $add(), $remove(), and $save() etc.

Below are two ways to update the local array to Firebase database:

<li ng-repeat="student in students">
  <input type="text" ng-model="student.name" ng-change="students.$save(student)" />
</li>

Another way

<li ng-repeat="student in students">
      <input type="text" ng-model="student.name" ng-change="update(student)" />
</li>

$scope.update = function(item) {
    return $scope.students.$save(item);
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM