2016-02-22 39 views
0

我是Angular JS的新手。正在开发一款游戏。其中一个需求是在超时时调用一个函数。在做研究时,我看到有$timeoutAngularJS提供,我必须将$timeout注入到控制器中,我试图这样做。但由于某种原因,我不确定这似乎不起作用!或者我不知道如何调试?

var ObstacleApp = angular.module('ObstacleApp', []) 
.controller('GameCtrl',function($scope,$timeout,game) { 
$scope.game = game; 
}); 

ObstacleApp.factory('game', function() { 
var tileNames = ['true', 'false', 'true', 'false', 'true','true','true','false','true','true','true','false','true','true','true','true','true','true','true','true']; 

return new Game(tileNames); 
}); 


    function Game(tileNames) 
    { 
    // omitted code 

    this.showOnClick = function() 
    { 
     // omitted code 

     $timeout(callOnTimeout,6000); 
    } 
    } 

编辑: 我已经通过@STEVER提到的变化 - 但它似乎不起作用。
当我Inspect控制台我看

Error: Unknown provider: $timeoutProvider <- $timeout 
at Error (native) 
at file:///home/rakshith/Desktop/Obstaclegame/lib/angular/angular.js:2492:15 
at Object.getService [as get] (file:///home/rakshith/Desktop/Obstaclegame/lib/angular/angular.js:2620:39) 
at file:///home/rakshith/Desktop/Obstaclegame/lib/angular/angular.js:2497:45 
at getService (file:///home/rakshith/Desktop/Obstaclegame/lib/angular/angular.js:2620:39) 
at invoke (file:///home/rakshith/Desktop/Obstaclegame/lib/angular/angular.js:2650:13) 
at Object.instantiate (file:///home/rakshith/Desktop/Obstaclegame/lib/angular/angular.js:2677:23) 
at file:///home/rakshith/Desktop/Obstaclegame/lib/angular/angular.js:4354:24 
at file:///home/rakshith/Desktop/Obstaclegame/lib/angular/angular.js:3986:17 
at forEach (file:///home/rakshith/Desktop/Obstaclegame/lib/angular/angular.js:118:20)(anonymous function) @ angular.js:5240 
+0

其中'Game'功能属于? –

回答

0

你忘了注入$timeout到您的工厂,像这样:

ObstacleApp.factory('game', function($timeout) { 
0
ObstacleApp.factory('game', function($timeout) { 
    //... 
    return new Game(tileNames, $timeout); 
}); 


    function Game(tileNames, $timeout) 
    { 
    // omitted code 

    this.showOnClick = function() 
    { 
     // omitted code 

     $timeout(callOnTimeout,6000); 
    } 
    } 
+0

进行了这些更改。但似乎有一些错误。我编辑了我的问题 - 包含我在控制台上看到的错误。 –

相关问题