Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度-ts文件中的ngxTranslate_Angular - Fatal编程技术网

Angular 角度-ts文件中的ngxTranslate

Angular 角度-ts文件中的ngxTranslate,angular,Angular,我需要使用翻译ts文件中的文本字符串 我添加了翻译服务,并尝试做类似的事情 constructor( translate: TranslateService ) { translate.get("hello.world").subscribe((text: string) => { console.log(text); // WORK BUT I NEED TO ADD THIS IN ARRAY }); this.idiomasTodos

我需要使用翻译ts文件中的文本字符串

我添加了翻译服务,并尝试做类似的事情

constructor(
    translate: TranslateService
  ) {
    translate.get("hello.world").subscribe((text: string) => {
      console.log(text); // WORK BUT I NEED TO ADD THIS IN ARRAY
    });

    this.idiomasTodos = [
      { id: 1, descripcion: translate.get("spanish") }, //THIS OTHER WAY NO WORK
      { id: 2, descripcion: translate.get("basque") }, //THIS OTHER WAY NO WORK
      { id: 3, descripcion: translate.get("english") } //THIS OTHER WAY NO WORK
    ];

那么,如何在数组中直接翻译任何文本字符串?

而不是
get
方法使用
instant
方法

    constructor(
        translate: TranslateService
      ) {
        this.idiomasTodos = [
          { id: 1, descripcion: translate.instant("spanish") }, 
          { id: 2, descripcion: translate.instant("basque") },
          { id: 3, descripcion: translate.instant("english") } 
        ];
    }

另一种方法是直接在视图中翻译每个单词,而不是在组件中使用
translate
pipe:


  • {{idiomasTodo.description | translate}}