2014-10-22 42 views
0

希望我不復制現有的問題... 我試圖訪問附加到「我的服務」中的「this」的函數,但是我必須從內部執行此操作jQuery的事件綁定。訪問jQuery代碼中的角度範圍

但我從角度得到「undefined不是函數」的錯誤。

代碼:

app.service("whatever",function(){ 

this.instantiatePreIds = function() { 
    /**whatever code**/ 
} 

this.checkOnLoad = function() { 

    angular.element(window).bind("popstate", function (e) { 

     this.instantiatePreIds(); /***this row throws that error***/ 

    }); 
} 
}); 

有什麼建議?謝謝。

回答

0

好吧,我知道了! 我所要做的就是將範圍的引用捕獲到一個var中,並用它來代替。

/*inside the service*/ 

var ref=this; 

this.checkOnLoad = function() { 

    angular.element(window).bind("popstate", function (e) { 

     ref.instantiatePreIds(); 

    }); 
} 

Yesss!