【发布时间】:2015-12-02 14:05:42
【问题描述】:
在类构造函数 angular 调用指令的链接函数后,我正在用 es6 编写指令(并用 babel 编译它),但由于某种原因,this 为空。
代码sn-p:
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());
【问题讨论】:
标签: angularjs angularjs-directive ecmascript-6 babeljs