【问题标题】:$firebase undefined in AngularJS$firebase 在 AngularJS 中未定义
【发布时间】:2014-02-02 10:21:26
【问题描述】:

由于某种原因,当我尝试使用 $firebase 时,它​​是未定义的。我对 Angular 还是很陌生,所以我猜我拆分文件的方式有些脱节。如果它只在一个 HTML 页面中,我可以让所有代码正常工作,但我想将它们全部拆分,因为我的应用程序将有不同的页面。

controllers.js

angular.module('myApp.controllers', ['firebase']).
  controller('StoryController', [function($scope, $firebase) {
    var storiesRef = new Firebase(FIREBASE_URL);
    $scope.stories = $firebase(storiesRef);
    $scope.addStory = function(e) {
      if (e.keyCode != 13) return;
      $scope.stories.$add({title: $scope.title, text: $scope.text});
      $scope.text = "";
    }
  }]);

app.js

angular.module('myApp', [
  'ngRoute',
  'myApp.filters',
  'myApp.services',
  'myApp.directives',
  'myApp.controllers'
]).
config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/stories', {templateUrl: 'partials/stories.html'});
  $routeProvider.when('/addStory', {templateUrl: 'partials/addStory.html', controller: 'StoryController'});
  $routeProvider.otherwise({redirectTo: '/'});
}]);

编辑:忘记包含错误。

TypeError: undefined is not a function
    at new <anonymous> (http://localhost:8000/app/js/controllers.js:8:22)
    at d (http://localhost:8000/app/lib/angular/angular.min.js:30:452)
    at Object.instantiate (http://localhost:8000/app/lib/angular/angular.min.js:31:80)
    at http://localhost:8000/app/lib/angular/angular.min.js:62:23
    at link (http://localhost:8000/app/lib/angular-route/angular-route.min.js:7:208)
    at F (http://localhost:8000/app/lib/angular/angular.min.js:49:345)
    at f (http://localhost:8000/app/lib/angular/angular.min.js:42:399)
    at http://localhost:8000/app/lib/angular/angular.min.js:42:67
    at http://localhost:8000/app/lib/angular/angular.min.js:43:299
    at z (http://localhost:8000/app/lib/angular/angular.min.js:47:23) <div ng-view="" class="ng-scope">

我在主模板的末尾包含了所有脚本。

【问题讨论】:

  • 有错误输出吗?您是否包含了必要的脚本?
  • 刚刚添加了错误输出,是的,所有必需的脚本都包括在内。当一切都在一个巨大的文件中时,一切都很好。我只是无法将其全部拆分以使其易于管理。
  • js文件是否缩小了?

标签: javascript angularjs firebase angularfire


【解决方案1】:

您正在使用内联依赖项,但未正确定义它们;

controller('StoryController', ['$scope', '$firebase', function($scope, $firebase) {

在此处阅读有关依赖注入的更多信息; http://docs.angularjs.org/guide/di

【讨论】:

  • 我知道这很简单!谢谢!
猜你喜欢
  • 1970-01-01
  • 2014-04-08
  • 2015-10-10
  • 2015-01-26
  • 1970-01-01
  • 2018-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多