当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Angular10 getLocaleCurrencyName()用法及代码示例


在本文中,我们将了解 Angular 10 中的 getLocaleCurrencyName 是什么以及如何使用它。

getLocaleCurrencyName用于获取给定语言环境的货币名称。

用法:

getLocaleCurrencyName(locale:string):string | null

NgModule:getLocaleCurrencyName 使用的模块是:

  • CommonModule

方法:



  • 创建要使用的 angular 应用程序
  • 在 app.module.ts 中导入 LOCALE_ID 因为我们需要导入语言环境才能使用 get getLocaleCurrencyName 。
import { LOCALE_ID, NgModule } from '@angular/core';
  • 在 app.component.ts 中导入 getLocaleCurrencyName 和 LOCALE_ID
  • 将 LOCALE_ID 作为公共变量注入,并使用语言环境变量编写获取一周第一天的代码。
  • 在 app.component.html 中使用字符串插值显示局部变量
  • 使用 ng serve 为 angular 应用程序提供服务以查看输出。

参数:

  • locale:包含带有规则的区域设置代码的字符串。

返回值:

  • string:包含货币名称的字符串。

范例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,
        getLocaleCurrencyName, 
        TranslationWidth } 
        from '@angular/common';
  
import {Component, 
        Inject,OnInit, 
        LOCALE_ID } 
        from '@angular/core';
  
@Component({
    selector:'app-root',
    templateUrl:'./app.component.html'
})
export class AppComponent {
    curr = getLocaleCurrencyName(this.locale);
    constructor(
        @Inject(LOCALE_ID) public locale:string,){}
      }

应用程序组件.html


<h1>
   GeeksforGeeks
</h1>
<p>Locale Currency Name is: 
   {{curr}}
</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,
        getLocaleCurrencyName, 
        TranslationWidth } 
        from '@angular/common';
  
import { Component, 
        Inject,OnInit, 
        LOCALE_ID } 
        from '@angular/core';
  
@Component({
    selector:'app-root',
    templateUrl:'./app.component.html'
})
export class AppComponent {
    curr = getLocaleCurrencyName(this.locale);
    constructor(
        @Inject(LOCALE_ID) public locale:string,){}
      }

应用程序组件.html


<h1>
   GeeksforGeeks
</h1>
<p>Currency Name is:{{curr}}, 
   example:( {{100 | currency}})
</p>

输出:

参考: https://angular.io/api/common/getLocaleCurrencyName 





注:本文由纯净天空筛选整理自GeeksforGeeks大神的英文原创作品 Angular10 getLocaleCurrencyName() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。