AngularJS网站示例项目中的Firebase API问题

时间:2015-03-09 13:02:33

标签: javascript angularjs firebase angularfire

开始潜入AngularJS,所以我去了他们的网站,但卡在Angular使用Firebase的wire up a backend部分。第一个问题来自依赖的排序:

angular.module('project', ['ngRoute', 'firebase'])

更改为

angular.module('project', ['firebase', 'ngRoute'])

但是现在它告诉我$add$scope.save电话中未定义。

类似的$add undefined个问题是herehere,但似乎都不适用。

注意:我正在运行节点http-server,所以我假设它不是本地主机问题。

脚本

angular.module('project', ['firebase', 'ngRoute'])

.value('fbURL', 'https://unique-url-yay.firebaseio.com/')
.service('fbRef', function(fbURL) {
  return new Firebase(fbURL)
})
.service('fbAuth', function($q, $firebase, $firebaseAuth, fbRef) {
  var auth;
  return function () {
      if (auth) return $q.when(auth);
      var authObj = $firebaseAuth(fbRef);
      if (authObj.$getAuth()) {
        return $q.when(auth = authObj.$getAuth());
      }
      var deferred = $q.defer();
      authObj.$authAnonymously().then(function(authData) {
          auth = authData;
          deferred.resolve(authData);
      });
      return deferred.promise;
  }
})

.service('Projects', function($q, $firebase, fbRef, fbAuth) {
  var self = this;
  this.fetch = function () {
    if (this.projects) return $q.when(this.projects);
    return fbAuth().then(function(auth) {
      var deferred = $q.defer();
      var ref = fbRef.child('projects/' + auth.auth.uid);
      var $projects = $firebase(ref);
      ref.on('value', function(snapshot) {
        if (snapshot.val() === null) {
          $projects.$set(window.projectsArray);
        }
        self.projects = $projects.$asArray();
        deferred.resolve(self.projects);
      });
      return deferred.promise;
    });
  };
})

.config(function($routeProvider) {
  $routeProvider
    .when('/', {
      controller:'ListCtrl',
      templateUrl:'list.html',
      resolve: {
        projects: function (Projects) {
          return Projects.fetch();
        }
      }
    })
    .when('/edit/:projectId', {
      controller:'EditCtrl',
      templateUrl:'detail.html'
    })
    .when('/new', {
      controller:'CreateCtrl',
      templateUrl:'detail.html'
    })
    .otherwise({
      redirectTo:'/'
    });
})

.controller('ListCtrl', function($scope, projects) {
  $scope.projects = projects;
})

.controller('CreateCtrl', function($scope, $location, Projects) {
  $scope.save = function() {
      Projects.projects.$add($scope.project).then(function(data) {
          $location.path('/');
      });
  };
})

.controller('EditCtrl',
  function($scope, $location, $routeParams, Projects) {
    var projectId = $routeParams.projectId,
        projectIndex;

    $scope.projects = Projects.projects;
    projectIndex = $scope.projects.$indexFor(projectId);
    $scope.project = $scope.projects[projectIndex];

    $scope.destroy = function() {
        $scope.projects.$remove($scope.project).then(function(data) {
            $location.path('/');
        });
    };

    $scope.save = function() {
        $scope.projects.$save($scope.project).then(function(data) {
           $location.path('/');
        });
    };
});

的index.html

<!DOCTYPE html>
<html>
  <head>
    <title>Angular</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-resource.min.js">
    </script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-route.min.js">
   </script>
    <script src="https://cdn.firebase.com/js/client/2.0.4/firebase.js"></script>
    <script src="https://cdn.firebase.com/libs/angularfire/0.9.0/angularfire.min.js"></script>
    <script src="scripts.js" type="text/javascript"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body style="padding:20px;">
    <div ng-app="project" class="ng-scope"></div>
      <div ng-view></div>
  </body>
</html>

1 个答案:

答案 0 :(得分:2)

好的,这里有一些事情要发生:

建议

  1. AngularFire已更新为1.1.1,现已弃用$firebase。请改用$firebaseObject$firebaseArray

  2. 您无需在Projects服务中执行所有操作并返回承诺。 $firebaseObject$firebaseArray返回承诺。

  3. 实施例

    查看此PLNKR我展示了您要完成的工作版本。

    • 它与我的一个公开Firebase实例相关联。
    • 您可以创建新的数据并在主页上查看。

    的JavaScript:

    (function(angular) {
      angular.module('project', ['firebase', 'ngRoute'])
      .value('fbURL', 'https://sb-plnkr.firebaseio.com/so:28942661')
    
      .service('fbRef', function(fbURL) {
        return new Firebase(fbURL)
      })
    
      .service('fbAuth', function($q, $firebaseAuth, fbRef) {
        var auth;
        return function () {
          if (auth) return $q.when(auth);
          var authObj = $firebaseAuth(fbRef);
          if (authObj.$getAuth()) {
            return $q.when(auth = authObj.$getAuth());
          }
          var deferred = $q.defer();
          authObj.$authAnonymously().then(function(authData) {
              auth = authData;
              deferred.resolve(authData);
          });
          return deferred.promise;
        }
      })
    
      .service('Projects', function($q, $firebaseArray, fbRef) {
        this.sync = $firebaseArray(fbRef);
        this.sync.$loaded().then(function(data) {
          var projects = data;
        });
        return this.sync;
      })
    
      .controller('ListCtrl', function($scope, $location, Projects) {
        Projects.$loaded().then(function(data){
          $scope.projects = data;
        });
      })
    
      .controller('CreateCtrl', function($scope, $location, Projects) {
        console.log("CreateCtrl");
        $scope.save = function() {
          console.debug("Adding");
          if ($scope.project && $scope.project.content !== '') {
            Projects.$add($scope.project).then(function(ref) {
              console.log("Added ref",ref);
              $location.path('/');
            }).catch(function(errorObject){
              console.error(errorObject);
            });
          } else {
            alert("You have to enter something.");
          }
        };
      })
    
      .controller('EditCtrl',function($scope, $location, $routeParams, Projects) {
        var projectId = $routeParams.projectId,
            projectIndex;
    
        $scope.init = function(){
          Projects.$loaded().then(function(data) {
            $scope.projects = data;
            console.info("EditCtrl - Projects.$loaded():",data);
            projectIndex = $scope.projects.$indexFor(projectId);
            $scope.project = $scope.projects[projectIndex];
          });
        }
    
        $scope.destroy = function() {
          $scope.projects.$remove($scope.project).then(function(data) {
            $location.path('/');
          });
        };
    
        $scope.save = function() {
          $scope.projects.$save($scope.project).then(function(data) {
           $location.path('/');
          });
        };
      })
    
      .config(function($routeProvider, $locationProvider) {
        $routeProvider
          .when('/', {
            controller:'ListCtrl',
            templateUrl:'list.html'
          })
          .when('/edit/:projectId', {
            controller:'EditCtrl',
            templateUrl:'detail.html'
          })
          .when('/new', {
            controller:'CreateCtrl',
            templateUrl:'create.html'
          })
          .otherwise({
            redirectTo:'/'
          });
    
          $locationProvider.html5Mode(true);
      });
    })(window.angular);
    

    HTML:

    (index.html的)

    <body style="padding:20px;">
      <div ng-app="project" ng-controller="EditCtrl">
        <a href="new">New</a>
        <div ng-view></div>
      </div>
    </body>
    

    (create.html上)

    <h2>Create</h2>
    <button ng-click="save()">Save</button>
    

    (list.html)

    <h2>List</h2>
    <div ng-repeat="(key,data) in projects">
      <span>$scope.projects[<span ng-bind="key"></span>].content : </span>
      <span ng-bind="data.content"></span>
    </div>
    <h2>Object Debug</h2>
    <pre ng-bind="projects | json"></pre>
    

    希望有所帮助!