在功能内部使用服务 [英] use a service inside a funcion

查看:127
本文介绍了在功能内部使用服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用图表和角度图.

I'm using highcharts and angular.

此刻,我想在点格式化程序函数中使用translateService:

At this moment I want to use translateService inside the point formatter function:

constructor(private translateService: TranslateService) {
}
private getOptions() {
  return {
    name: this.name,
    color: this.color,
    tooltip: {
      pointFormatter: function () {
        /* this line is my problem, how to use this.translateService here! */
        const myTranslation = translateService.instant('KEY', {value:this.y});
        return `<span>\u25CF</span> ${myTranslation}`;
      }
    },
  };
}

推荐答案

您可以使用IIFE从外部范围存储this:

You can use IIFE to store this from the outer scope:

tooltip: {
  pointFormatter: (function(component) {
    return function() {
      console.log(this, component);
    }
  })(this)
}


实时演示: http://jsfiddle.net/BlackLabel/6m4e8x0y/4918/

这篇关于在功能内部使用服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆