如何为数组填充一个下拉列表,其中值已被外部化[Angular]

我认为我不太了解外部化概念。我有一个简单的下拉菜单,其选项存储在相应的打字稿文件中。

HTML

<select>
  <option *ngFor="let source of modeOptions" [value]="source">
    {{ source }}
  </option>
</select>

打字稿

modeOptions= ['Calendar year','Current quarter','Rolling year'];

但是现在我决定将所有值都外部化。

en-Us.json

{
  "Modes": {
    "CalendarYear": "Calendar year","YearToDate": "Year-to-date","Rolling12Months": "Rolling 12 months","CurrentQuarter": "Current quarter"
  }
}

打字稿

import { Component,OnInit } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';

@Component({
    ...
})
export class TimeselectorComponent implements OnInit {

    mode = 'Calendar year';

    public sources = [];
    public modeOptions = [];

    constructor(private translate: TranslateService) {}

    translateCard(): void {
      this.translate
        .get([
          'Months.CalendarYear','Months.YearToDate','Months.Rolling12Months','Months.CurrentQuarter'
        ])
        .subscribe(translations => {
          this.sources.push(translations['Months.CalendarYear']);
          this.sources.push(translations['Months.YearToDate']);
          this.sources.push(translations['Months.Rolling12Months']);
          this.sources.push(translations['Months.April']);
          this.sources.push(translations['Months.CurrentQuarter']);

          // PROBLEM IS HERE
          this.modeOptions = Array.from({ length: Math.ceil(this.sources.length) },(_,i) => i).map(i =>
                    this.modeOptions.map(x => ({

                    }))
                );
          console.log("Modes are: "+this.modeOptions); //OUTPUT: Modes are:,});
    }

    ngOnInit(): void {
        this.translateCard();
    }
}

问题在于填充我的选项数组。这就是为什么我的下拉列表为空。它没有显示任何选项。我之前也犯了类似的错误,但这是Dropdown之外的其他组件。我想我误解了这个概念。

w1931079www 回答:如何为数组填充一个下拉列表,其中值已被外部化[Angular]

您当前的尝试存在很多问题。

  1. 首先,下载所有必需的依赖项:

    "@ngx-translate/http-loader": "4.0.0" 
    "@ngx-translate/core": "11.0.1",
  2. 接下来,在app.module中设置加载程序

    @NgModule({
      imports: [
        ...
        HttpClientModule,TranslateModule.forRoot({
          loader: {
            provide: TranslateLoader,useFactory: HttpLoaderFactory,deps: [HttpClient]
          }
        })
      ]
    })
    export class AppModule {}
    

    定义加载程序

    export function HttpLoaderFactory(httpClient: HttpClient) {
      return new TranslateHttpLoader(httpClient);
    }
    
  3. 在app.component.ts中确定哪个[lang].json文件 您需要加载

      constructor(private translate: TranslateService) {
        translate.setDefaultLang("en");
        translate.use("en");
      }
    
  4. 该组件中的

  5. 现在可以确定i18n文件中的哪些 keys 是必需的。

      ngOnInit() {
        this.sources = [
          "Modes.CalendarYear","Modes.YearToDate","Modes.Rolling12Months","Modes.CurrentQuarter"
        ];
      }
    

    并在模板上利用translate管道 国际化这些价值观。

    <select [(ngModel)]="mode" name="source">
      <option *ngFor="let source of sources" [value]="source">
        {{ source | translate }} 
      </option>
    </select>
    

Working stackblitz以上步骤。

本文链接:https://www.f2er.com/2901708.html

大家都在问