In diesem Artikel werden wir sehen, was getLocaleDayNames in Angular 10 ist und wie es verwendet wird.

getLocaleDayNames wird verwendet, um Wochentage für das angegebene Gebietsschema abzurufen.

Syntax:

getLocaleDayNames(
             locale: string, 
             formStyle: FormStyle, 
             width: TranslationWidth
             )

NgModule: Das von DayNames verwendete Modul ist:

  • GemeinsamesModul

Sich nähern: 

  • Erstellen Sie die zu verwendende Winkel-App
  • Importieren Sie in app.module.ts LOCALE_ID, da das Gebietsschema importiert werden muss, um get getLocaleDayNames zu verwenden.
import { LOCALE_ID, NgModule } from '@angular/core';
  • Importieren Sie in app.component.ts FormStyle, getLocaleDayNames, TranslationWidth und LOCALE_ID
  • Fügen Sie LOCALE_ID als öffentliche Variable ein und schreiben Sie den Code zum Abrufen der Wochentage mithilfe der Gebietsschemavariablen.
  • Zeigen Sie in app.component.html die lokale Variable mit Zeichenfolgeninterpolation an
  • bedienen Sie die Winkel-App mit ng serve, um die Ausgabe anzuzeigen.

Parameter:

  • locale: Ein Gebietsschemacode mit Regeln.
  • formStyle: Der Formularstil, in dem Tage angezeigt werden sollen
  • Breite: Die Gesamtbreite genommen.

Rückgabewert :

  • array: ein Array von Tagesnamen.

Beispiel 1:

app.module.ts

import { LOCALE_ID, NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
  
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
  
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [
      { provide: LOCALE_ID, useValue: 'en-GB' },
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts

import {FormStyle,
        getLocaleDayNames, 
        TranslationWidth } 
        from '@angular/common';
  
import {Component, 
        Inject,OnInit, 
        LOCALE_ID } 
        from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
export class AppComponent {
    day = getLocaleDayNames(
    this.locale, 
    FormStyle.Standalone, 
    TranslationWidth.Wide
    );
    constructor(
        @Inject(LOCALE_ID) public locale: string,){}
      }

app.component.html

<h1>
  GeeksforGeeks
</h1>
<p>Days of week are : {{day}}</p>

Ausgabe:

Beispiel 2:

app.module.ts

import { LOCALE_ID, NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
  
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
  
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [
      { provide: LOCALE_ID, useValue: 'en-GB' },
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts

import {FormStyle,
        getLocaleDayNames, 
        TranslationWidth } 
        from '@angular/common';
  
import {Component, 
        Inject, 
        LOCALE_ID } 
        from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
export class AppComponent {
    day = getLocaleDayNames(this.locale, 
    FormStyle.Standalone, 
    TranslationWidth.Wide);
    constructor(
        @Inject(LOCALE_ID) public locale: string,){
            console.log(this.day)
        }
      }

Ausgabe:

Referenz: https://angular.io/api/common/getLocaleDayNames