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


Angular10 getLocaleFirstDayOfWeek()用法及代码示例


在本文中,我们将了解 Angular 10 中的 getLocaleFirstDayOfWeek 是什么以及如何使用它。 getLocaleFirstDayOfWeek 用于获取给定语言环境的一周的第一天。

用法:

getLocaleFirstDayOfWeek(locale:string):WeekDay

NgModule:FirstDayOfWeek 使用的模块是:

  • CommonModule

方法:

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



参数:

  • locale:带有规则的区域设置代码。

返回值:

  • number:索引从 0 开始。

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

应用程序组件.html


<h1>
  GeeksforGeeks
</h1>
  
<p>First Days of week:{{dd}}</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 {getLocaleFirstDayOfWeek } from '@angular/common';
  
import { Component, Inject, LOCALE_ID } from '@angular/core';
  
@Component({
    selector:'app-root',
    templateUrl:'./app.component.html'
})
export class AppComponent {
    day = getLocaleFirstDayOfWeek(this.locale);
    constructor(
        @Inject(LOCALE_ID) public locale:string,){
        }
}

应用程序组件.html


<h1>
  GeeksforGeeks
</h1>
  
<p>index value of first Day of week:{{day}}</p>

输出:

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




相关用法


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