AngularJS & Firebase '$timeout is not defined&

2019-08-09 10:01发布

Hey guys I have just been learning about angular JS and Firebase and for some reason I seem to be getting a Reference Error when I try to call the $timeout function in the following code:

'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();
      });
    });
  });

I get this exact error:

Uncaught ReferenceError: $timeout is not defined(anonymous function) @ main.js:16(anonymous function) @ firebase.js:202gc @ firebase.js:52cc @ firebase.js:30dc @ firebase.js:29h.Kb @ firebase.js:221h.Ld @ firebase.js:189Fh.Ld @ firebase.js:179(anonymous function) @ firebase.js:177zh @ firebase.js:171La.onmessage @ firebase.js:170

Any idea why this might be happening? Thanks, Nick

1条回答
老娘就宠你
2楼-- · 2019-08-09 10:34

You need declare $timeout to use it same as:

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();
      });
    });
  });
查看更多
登录 后发表回答