0

不確定是否所有東西都在這裏正確連線。我們的目標是調用nextSlide函數,讓它更改類,等待一秒鐘,然後將當前圖片加1,然後將類更改回去。我認爲問題在於範圍,但是我沒有在我的代碼中的任何地方使用$ scope,並且它的所有示例都使用它。超時在運行時完全跳過。

如何獲得$超時執行?

var app = angular.module('cole', []); 
app.directive('slideShow',['$timeout', function() { 
    return{ 
     restrict: 'E', 
     templateUrl: 'slide-show.html', 
     controller: function(){ 


this.nextSlide = function() { 

       document.getElementById("frontPic").className = "fadeOut"; 

        this.$timeout(function() { 

        this.setCurrent(this.current + 1); 

        document.getElementById("frontPic").className = "picFront"; 

       }, 1000); 
      }; 

     }, 
     controllerAs: 'pics' 
    }; 
}]); 

完整的代碼是在這裏https://github.com/Cameron64/Angular

回答

2

沒有,$timeout是封閉函數的參數,而不是this成員。正確的代碼是:

app.directive('slideShow',['$timeout', function($timeout) { 
    ... 
    $timeout(function() { 
     ... 

也格外注意:因爲它的立場既不setCurrent()this成員,即控制器對象。它可能也需要更正。

+0

啊,剛剛扔了一個 var show = this; 隨着你的答案,它的工作!謝謝 – 2014-09-02 02:34:26

相關問題