在本文中,我們將了解 Angular 10 中的 getLocaleEraNames 是什麽以及如何使用它。
getLocaleEraNames是用於獲取給定語言環境的 Gregorian-calendar 紀元。
用法:
getLocaleEraNames(locale:string, width:TranslationWidth)
NgModule:getLocaleEraNames 使用的模塊是:
- CommonModule
方法:
- 創建要使用的 angular 應用程序
- 在 app.module.ts 中導入 LOCALE_ID,因為我們需要導入語言環境以使用 getLocaleEraNames。
import { LOCALE_ID, NgModule } from '@angular/core';
- 在 app.component.ts 中導入 getLocaleEraNames 和 LOCALE_ID
- 將 LOCALE_ID 作為公共變量注入。
- 在 app.component.html 中使用字符串插值顯示局部變量
- 使用 ng serve 為 angular 應用程序提供服務以查看輸出。
參數:
- locale:包含帶有規則的區域設置代碼的字符串。
- width:字符寬度。
返回值:
- array:包含時代名稱的數組。
範例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,
getLocaleEraNames,
TranslationWidth }
from '@angular/common';
import {Component,
Inject,OnInit,
LOCALE_ID }
from '@angular/core';
@Component({
selector:'app-root',
templateUrl:'./app.component.html'
})
export class AppComponent {
code = getLocaleEraNames(this.locale,
TranslationWidth.Wide);
constructor(
@Inject(LOCALE_ID) public locale:string,){}
}
應用程序組件.html
<h1>
GeeksforGeeks
</h1>
<p>Era Names is:{{code}}</p>
輸出:
範例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,
getLocaleEraNames,
TranslationWidth }
from '@angular/common';
import {Component,
Inject,
OnInit,
LOCALE_ID }
from '@angular/core';
@Component({
selector:'app-root',
templateUrl:'./app.component.html'
})
export class AppComponent {
era = getLocaleEraNames(this.locale,
TranslationWidth.Wide);
a='';
b='';
constructor(
@Inject(LOCALE_ID) public locale:string,){
if(this.era[0]=='Before Christ'){
this.a='BC';
this.b='AD';
}
}
}
應用程序組件.html
<h1>
GeeksforGeeks
</h1>
<p>Era Names:
<li>{{a}}</li>
<li>{{b}}</li>
</p>
輸出:
參考: https://angular.io/api/common/getLocaleEraNames
相關用法
- Angular10 getLocaleDirection()用法及代碼示例
- Angular10 getLocaleCurrencySymbol()用法及代碼示例
- Angular10 getLocaleCurrencyName()用法及代碼示例
- Angular10 getLocaleDateFormat()用法及代碼示例
- Angular10 getLocaleDateTimeFormat()用法及代碼示例
- Angular10 getNumberOfCurrencyDigits()用法及代碼示例
- Angular10 getLocaleId()用法及代碼示例
- Angular10 isPlatformBrowser()用法及代碼示例
- Angular10 isPlatformServer()用法及代碼示例
- Angular10 Animation animate()用法及代碼示例
- Angular10 getLocaleCurrencyCode()用法及代碼示例
- Angular10 getLocaleWeekEndRange()用法及代碼示例
- Angular10 getLocaleFirstDayOfWeek()用法及代碼示例
- Angular10 getLocaleDayNames()用法及代碼示例
- Angular10 getLocaleDayPeriods()用法及代碼示例
- Angular10 getLocaleMonthNames()用法及代碼示例
注:本文由純淨天空篩選整理自taran910大神的英文原創作品 Angular10 getLocaleEraNames() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。