如何使用以下代码在firebase中进行合并

时间:2015-06-16 13:28:30

标签: firebase angularfire

如何在firebase中绑定这些代码并进行自动同步,我想检查有人参加的被邀请者

var guestApp = angular.module("guestApp", []);

guestApp.controller('guestCtrl', function($scope) {
    $scope.guests = ['jean','elie','tierry'];
   
  $scope.addGuest = function(){
  $scope.guests.push($scope.newGuest);
  $scope.newGuest = '';

 };
 
 $scope.removeGuest = function(guest){
 var i = $scope.guests.indexOf(guest);
 $scope.guests.splice(i,1);
 };

1 个答案:

答案 0 :(得分:1)

首先,您需要确保HTML中包含这些库:

<!-- AngularJS -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>    
<!-- Firebase -->
<script src="https://cdn.firebase.com/js/client/2.2.4/firebase.js"></script>
<!-- AngularFire -->
<script src="https://cdn.firebase.com/libs/angularfire/1.1.1/angularfire.min.js"></script>

至于代码本身:

  1. 您需要添加firebase模块依赖项:

    var guestApp = angular.module("guestApp", ["firebase"]);
    
  2. 然后,在您的控制器中,您需要注入适当的firebase服务并记录身份验证:

    guestApp.controller('guestCtrl', function($scope, $firebaseArray) {
        var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/guests");
    
  3. 之后,您可以将范围变量绑定到firebase引用:

        $scope.guests = $firebaseArray(ref);
    
  4. 至于添加新项目:

      $scope.addGuest = function(){
          $scope.guests.$add($scope.newGuest);
          $scope.newGuest = '';
      };
    
  5. 并删除:

     $scope.removeGuest = $scope.guests.$remove;
    
  6. 我想是的。顺便说一句。你可以在官方文档中阅读所有相关内容;) https://www.firebase.com/docs/web/libraries/angular/quickstart.html