Javascript 角度es6、this、scope和el在jqlite单击事件中都未定义
我无法使链接函数在ES6中工作。点击事件上根本没有上下文传递——我是否遗漏了一些明显的东西Javascript 角度es6、this、scope和el在jqlite单击事件中都未定义,javascript,angularjs,ecmascript-6,Javascript,Angularjs,Ecmascript 6,我无法使链接函数在ES6中工作。点击事件上根本没有上下文传递——我是否遗漏了一些明显的东西 export function TbeDirective() { 'ngInject'; let directive = { restrict: 'E', templateUrl: 'app/main/directives/template.html', controller: TheController,
export function TbeDirective() {
'ngInject';
let directive = {
restrict: 'E',
templateUrl: 'app/main/directives/template.html',
controller: TheController,
controllerAs: 'vm',
bindToController: true,
link: function(scope, el, attr, vm){
el.bind('keypress', function(){
var x = this.firstChild; //in the console this is the element,
//but when the code runs it is undefned
console.log(this); //undefined
console.log(TheDirective); //undefined
console.log(el); //undefined
console.log(vm); //undefined
});
}
};
return directive;
}
class TheController {
constructor ($scope) {
'ngInject';
}
}
}
控制器
类必须在其使用位置的上方定义
类
据我所知,es不会被提升。您使用的是一个没有上下文的arrow函数。没有arrow函数则无法工作。如果希望此
成为事件处理程序中的元素,则处理程序不能是arrow函数。对于链接
,使用箭头函数也没有意义。正如我所说,它在任何一种情况下都不起作用上下文在控制台中起作用,它在运行时实际上不起作用