如何显示创建的数据?

时间:2015-12-17 12:45:23

标签: javascript angularjs firebase angularfire

我发送数据......

....
        // upload on file select or drop
        $scope.upload = function (file, id) {
            id = typeof id !== 'undefined' ? id : null;
            Upload.base64DataUrl(file).then(function(base64){
                //auth
                var fbAuth = FirebaseURL.getAuth();
                //Ref
                var ref = FirebaseURL.child("users_photos");
                ref.push({'image': base64,'removed': true, 'user_id': fbAuth.uid, 'dt_created':Firebase.ServerValue.TIMESTAMP ,'dt_updated':Firebase.ServerValue.TIMESTAMP}, function(error){
                    if (error) {
                        alert('Error');
                    } else {
                        var newID = ref.key();

                         //I would like display data insert here?
                         console.log(DATA RESULT INSERT);
                    }   
                });
            });

我想显示插入的数据。

可以在没有按键查询的情况下显示最后插入的对象吗?

1 个答案:

答案 0 :(得分:2)

Use AngularFire for synchronized collections.

使用limitToLast(1)创建查询以始终同步最后插入的对象。

angular.module('app', ['firebase'])
  .constant('FirebaseUrl', '<my-firebase-app>')
  .service('rootRef', ['FirebaseUrl', Firebase)
  .factory('userItems', UserItems)
  .controller('MyCtrl', MyController);

function UserItems($firebaseArray, rootRef) {
  return function userItems(uid) {
    var itemRef = rootRef.child('items');
    var query = itemRef.orderyByChild('uid').equalTo(uid);
    return $firebaseArray(query);
  }
}

function MyController($scope, userItems, rootRef) {
  $scope.items = userItems(rootRef.getAuth().uid);
  $scope.addItem = function addItem(item) {
    $scope.items.$add(item).then(function(ref) {
      var record = $scope.items.$getRecord(ref.key());
      // save the data to the other structure
    });
  };
}

See the section on Complex queries for more info.