在$ on侦听器中调用'this'函数

时间:2017-12-24 15:16:31

标签: javascript angularjs scope this listener

如何在侦听器中正确调用此函数('this'对窗口范围的引用)?

this.openPackage = function (package) {
...
}

$rootScope.$on('open_Package', function (event, package) {
           openPackage(package); // doesn't work
});

1 个答案:

答案 0 :(得分:0)

只需使用箭头功能即可保持上下文:

 $rootScope.$on('open_Package',  (event, package) => {
       this.openPackage(package); // does work
 });