我正在用es6編寫指令(並且用babel編譯它)後類構造函數角度調用指令的鏈接函數,但由於某些原因this
爲空。在es6指令中爲null
代碼片段:
class AutoSaveDirective {
constructor($timeout) {
this.restrict = 'EA';
this.require = '^form';
this.$timeout = $timeout;
this.scope = {
autoOnSave: '&',
autoSaveDebounce: '='
}
}
link(scope, el, attr, formCtrl) {
scope.$watch(()=> {
console.log('form changed, starting timout');
if (!formCtrl.$dirty) {
return;
}
at this line ==>if(this.currentTimeout){
console.log('old timeout exist cleaning');
this.currentTimeout.cancel();
this.currentTimeout = null;
}
console.log('starting new timeout');
this.currentTimeout = $timeout(()=>{
console.log('timeout reached, initiating onsave')
scope.autoOnSave();
}, scope.autoSaveDebounce);
});
}
}
angular.module('sspApp').directive('autoSave',() => new AutoSaveDirective());