AngularJS & Firebase '$timeout is not defined'

断了今生、忘了曾经 提交于 2020-01-15 19:13:44

问题


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:


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


来源:https://stackoverflow.com/questions/35526153/angularjs-firebase-timeout-is-not-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!