AngularJS& Firebase' $超时未定义'

时间:2016-02-20 16:51:53

标签: javascript angularjs firebase yeoman

嘿伙计们我刚刚学习了角度JS和Firebase,出于某些原因,当我尝试在以下代码中调用$ timeout函数时,我似乎得到了一个引用错误:

transition: background-color 1s linear;

我得到了这个确切的错误:

'use strict';

/**
 * @ngdoc function
 * @name drivenApp.controller:MainCtrl
 * @description
 * # MainCtrl
 * Controller of the drivenApp
 */
angular.module('drivenApp')
  .controller('MainCtrl', function ($scope) {
    var rootRef = new Firebase('https://vivid-torch-5432.firebaseio.com/');
    var childRef = rootRef.child('message');

    childRef.on('value', function(snapshot){
      $timeout(function() {
        var snapshotVal = snapshot.val();
        console.log(snapshotVal);
        $scope.message = snapshot.val();
      });
    });
  });

知道为什么会这样吗?谢谢,尼克

1 个答案:

答案 0 :(得分:8)

您需要声明$timeout以使用它:

angular.module('drivenApp')
  .controller('MainCtrl', function ($scope, $timeout) {
    var rootRef = new Firebase('https://vivid-torch-5432.firebaseio.com/');
    var childRef = rootRef.child('message');

    childRef.on('value', function(snapshot){
      $timeout(function() {
        var snapshotVal = snapshot.val();
        console.log(snapshotVal);
        $scope.message = snapshot.val();
      });
    });
  });