2013-04-30 87 views

回答

7

這是因爲你綁定回調的方式。更改

service.$on('hello', service.method); 

service.$on('hello', function() { 
    service.method(); 
}); 

當你說spyOn(service, 'method'),你說「更換在service.method有間諜參考的價值。」但是,您的原始service.$on代碼不會在觸發事件時查找service.method 上的值 - 只需在服務初始化時查看。因此,更改service.method指向後面的引用不起作用。