2016-11-06 1 views
0

Hier habe ich eine Verzögerung in JavaScript Forloop mit $ Timeout hinzufügen. Unerwarteterweise habe ich eine Fehlermeldung erhalten, die besagt:
ReferenceError $ timeout ist nicht definiert. Ich bin neu bei angularjs bitte hilf mir. 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 
    } 

} 

Antwort

-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 
    } 

} 

Durch $timeout in der Reglerfunktion injizieren wir dieses Problem lösen können.

7

Sie müssen inject die $timeout in der Reglerfunktion.

function _controller($timeout) { ... } 

Bitte sehen aktualisiert Plunkr

+0

Ich habe versucht, aber nicht funktioniert – htoniv

+1

Ich habe meine Antwort mit einem Plunkr-Link aktualisiert :) –

Verwandte Themen