2016-11-06 2 views
0

여기에 $ timeout을 사용하여 javascript forloop에 지연을 추가합니다. 예기치 않게 오류가 발생했습니다.
ReferenceError $ timeout이 정의되지 않았습니다.. 나는 angularjs을 (를) 처음 사용했습니다. PLNKR


function CompLibrary() { 
    return { 
    init: init 
    } 
    function init(dependencies, controller) { 
    dependencies.push(controller); 
    angularApp.controller('MainCtrl', dependencies); 
    } 
} 
var compX = CompLibrary(); 
compX.init(deps, _controller); 
function _controller() { 
    var ViewModel = this; 
    ViewModel.search = "Name"; 
    ViewModel.quantity = 1; 

    for(var i = 0; i < 4; i++) { 
    (function(i){ 
     $timeout(function() { 
      ViewModel.quantity++; 
     }, i * 2000); 
    })(i); // Pass in i here 
    } 

} 

답변

-1
var deps = []; 
var angularApp = angular.module('plunker',[]); 
function CompLibrary() { 
    return { 
    init: init 
    } 
    function init(dependencies, controller) { 
    dependencies.push('$timeout'); 
    dependencies.push(controller); 
    angularApp.controller('MainCtrl', dependencies); 
    } 
} 
var compX = CompLibrary(); 
compX.init(deps, _controller); 
function _controller($timeout) { 
    var ViewModel = this; 
    ViewModel.search = "Name"; 
    ViewModel.quantity = 1; 

    for(var i = 0; i < 4; i++) { 
    (function(i){ 
     $timeout(function() { 
      ViewModel.quantity++; 
     }, i * 2000); 
    })(i); // Pass in i here 
    } 

} 

업데이트보십시오.

7

당신은 inject 컨트롤러 기능에 $timeout에 있습니다.

function _controller($timeout) { ... } 

우리는이 문제를 해결할 수있는 제어 기능에 $timeout 주입함으로써 Plunkr

+0

시도했지만 작동하지 않음 – htoniv

+1

Plunkr 링크로 답변을 업데이트했습니다. :) –

관련 문제