Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs Firebase数据标准化。如何获取基于此结构的集合?_Angularjs_Firebase_Angularjs Service_Angularfire - Fatal编程技术网

Angularjs Firebase数据标准化。如何获取基于此结构的集合?

Angularjs Firebase数据标准化。如何获取基于此结构的集合?,angularjs,firebase,angularjs-service,angularfire,Angularjs,Firebase,Angularjs Service,Angularfire,我想我快到了,我能够打印出属于某个用户的图书ID,但一直试图从firebase图书参考中获取属于某个用户的图书列表,但未成功 我将在这里大致遵循教程: 还可以在此处阅读有关数据非规范化的文档: 如果我想在一个页面中显示用户,然后显示其所有书籍,我应该怎么做 火基结构 看法 服务 首先更新到angularFire 0.6。这看起来像0.3.*英寸。angularFire已更改为$firebase,并且具有更强大和简化的界面 香草火碱 首先,我将以艰难的方式来做这件事,因为我认为理解这里的基本原

我想我快到了,我能够打印出属于某个用户的图书ID,但一直试图从firebase图书参考中获取属于某个用户的图书列表,但未成功

我将在这里大致遵循教程:

还可以在此处阅读有关数据非规范化的文档:

如果我想在一个页面中显示用户,然后显示其所有书籍,我应该怎么做

火基结构 看法 服务
首先更新到angularFire 0.6。这看起来像0.3.*英寸。angularFire已更改为
$firebase
,并且具有更强大和简化的界面

香草火碱

首先,我将以艰难的方式来做这件事,因为我认为理解这里的基本原则是非常有价值的。这相当复杂,我只讲基本内容。还有许多微小的边缘情况需要处理:

angular.module('app', [])
    .controller('UsersController', function($scope, $firebase, $timeout, $routeParams){
      var userId = $routeParams.userId;
      $scope.user = $firebase(new Firebase('URL/user/'+userId));

      // or, for 3-way binding and automatic writes back to Firebase
      var userRef = $firebase(new Firebase('URL/users/'+userId)).$bind($scope. 'user');

      // grab this users' books using Firebase (the hard way)
      $scope.books = {};
      var booksRef = new Firebase('URL/books/');

      // fetch the user's book list dynamically because it may change in real-time
      var indexRef = new Firebase('URL/user/'+userId+'/books');

      // watch the index for add events
      indexRef.on('child_added', function(indexSnap) {
         // fetch the book and put it into our list
         var bookId = indexSnap.name();
         booksRef.child(bookId).on('value', function(bookSnap) {
            // trigger $digest/$apply so Angular syncs the DOM
            $timeout(function() {
               if( snap.val() === null ) {
                  // the book was deleted
                  delete $scope.books[bookId];
               }
               else {
                  $scope.books[bookId] = snap.val();
               }
            });
         });
      });

      // watch the index for remove events
      indexRef.on('child_removed', function(snap) {
         // trigger $digest/$apply so Angular updates the DOM
         $timeout(function(snap) {
            delete $scope.books[snap.name()];
         });
      });
});
然后是HTML(这与下面的其他示例相同):

Firebase.util.join

是一个更强大、更复杂的用于规范化路径的库。因为它返回的对象与常规Firebase引用一样工作,所以它也可以无缝地与angularFire 0.5及更高版本一起使用

angular.module('app', [])
.controller('UsersController', function($scope, $firebase){
   var userId = $routeParams.userId;
   $scope.user = $firebase(new Firebase('URL/user/'+userId)); 

   var fb = new Firebase(URL);
   var ref = new Firebase.util.intersection( fb.child('user/'+userId+'/books'), fb.child('books') );

   // magic!
   $scope.books = $firebase(ref);
});

您使用的是什么版本的angularFire?为什么不是0.6呢?另外,Users.find返回什么?火基裁判?一根弦?@Kato AngularFire 0.3。我想在改变库和可能破坏某些东西之前,我应该先看看教程中的内容。Users.find返回并绑定$routeParams中指定的userId的用户数据,所以我很确定它是firebase引用?这是调用的输出,Object{then:function,catch:function,finally:function}哇,常规方法工作得很好!非常感谢您展示了常规方法,以便理解其原理。你知道他们怎么说教人钓鱼!现在我来看看其他库。从最佳实践来看,这些代码属于控制器还是服务?控制器适合学习和简单的用例。当代码将在控制器之间共享或控制器变长时,将代码移出服务。有一些有趣的工厂/服务。这是一个很好的例子,说明了在何处使用工厂,因为它将对
Firebase
的引用和您的命名空间URL抽象为一个点。您可以看到我认为
Firebase.util.intersection
不再存在,而是被
Firebase.util.NormalizedCollection
取代。
'use strict';

angular.module('ccApp.controllers.users', ['ccApp.services.users'])
    .controller('UsersController', ['$scope', '$routeParams', '$location', 'angularFire', 'Users',
      function($scope, $routeParams, $location, angularFire, Users){

      $scope.user = {};
      $scope.userId = $routeParams.userId;

      $scope.findOneUser = function(userId){
        if (!!$scope.userId){
          angularFire(Users.find($routeParams.userId), $scope, 'user');
        }
      };

      $scope.updatePhotoUrl = function(url, user){
        $scope.fileUrl = url;
        console.log($scope.fileUrl[0].url);
        user.photoUrl = $scope.fileUrl[0].url;
      };

      $scope.findUsers = function(){
        $scope.users = Users.collection();
      };

      $scope.findWholesalers = function(){
        $scope.wholesalers = Users.collection();
      };

    }]);
'use strict';

angular.module('ccApp.services.users', ['ccApp.services.firebaseRefs'])
  .factory('Users', ['angularFireCollection', 'FireRef',
    function(angularFireCollection, FireRef){
      return{
        collection: function(cb){
          return angularFireCollection(FireRef.users(), cb);
        }
      , find: function(userId){
          return FireRef.users().child('/'+userId);
        }
      };
  }]);
angular.module('app', [])
    .controller('UsersController', function($scope, $firebase, $timeout, $routeParams){
      var userId = $routeParams.userId;
      $scope.user = $firebase(new Firebase('URL/user/'+userId));

      // or, for 3-way binding and automatic writes back to Firebase
      var userRef = $firebase(new Firebase('URL/users/'+userId)).$bind($scope. 'user');

      // grab this users' books using Firebase (the hard way)
      $scope.books = {};
      var booksRef = new Firebase('URL/books/');

      // fetch the user's book list dynamically because it may change in real-time
      var indexRef = new Firebase('URL/user/'+userId+'/books');

      // watch the index for add events
      indexRef.on('child_added', function(indexSnap) {
         // fetch the book and put it into our list
         var bookId = indexSnap.name();
         booksRef.child(bookId).on('value', function(bookSnap) {
            // trigger $digest/$apply so Angular syncs the DOM
            $timeout(function() {
               if( snap.val() === null ) {
                  // the book was deleted
                  delete $scope.books[bookId];
               }
               else {
                  $scope.books[bookId] = snap.val();
               }
            });
         });
      });

      // watch the index for remove events
      indexRef.on('child_removed', function(snap) {
         // trigger $digest/$apply so Angular updates the DOM
         $timeout(function(snap) {
            delete $scope.books[snap.name()];
         });
      });
});
<div data-ng-repeat="(bookId, book) in books">
   {{bookId}}: {{book.title}}
</div>
angular.module('app', [])
.controller('UsersController', function($scope, $firebase, $timeout){
   var userId = $routeParams.userId;
   $scope.user = $firebase(new Firebase('URL/user/'+userId));

   var fb = new Firebase(URL);
   var index = new FirebaseIndex( fb.child('user/'+userId+'/books') );
   $scope.books = {};

   // almost magic
   index.on('child_added', function(snap) {
      $timeout(function() { $scope.books[snap.name()] = snap.val(); });
   });

   index.on('child_removed', function(snap) {
      $timeout(function() { delete $scope.books[snap.name()]; });
   });
});
angular.module('app', [])
.controller('UsersController', function($scope, $firebase){
   var userId = $routeParams.userId;
   $scope.user = $firebase(new Firebase('URL/user/'+userId)); 

   var fb = new Firebase(URL);
   var ref = new Firebase.util.intersection( fb.child('user/'+userId+'/books'), fb.child('books') );

   // magic!
   $scope.books = $firebase(ref);
});