我有两个相同的指令(除命名外),编译器在另一个指令中创建一个变量_this,但另一个指令则不然。它可能会有所不同,哪一个是好的,哪一个不是。 它们都有这样的链接功能:
link = (scope: IMyScope, element, attrs) => {
scope.setVariable1 = () => {
this.service.setVariable1(scope.variable1);
}
问题是当编译版本尝试使用this.service(undefined)而不是_this.service时。
答案 0 :(得分:0)
不完全确定您的编译可能存在什么问题,但通常我不会将指令声明为类,而是作为函数声明。
function yourDirective(yourService): ng.IDirective{
return {
//other directive properties
link: (scope: IMyScope, element, atrrs) =>{
scope.setVariable1 = () => {
yourService.setVariable1(scope.variable);
}
}
};
};
yourDirective.$inject = ["yourService"];
angular.module("app").directive("yourDirective", yourDirective);
然后,您不需要在链接功能中使用this
。