【问题标题】:How to use the getDay method in Ionic 2 / 3如何在 Ionic 2 / 3 中使用 getDay 方法
【发布时间】:2017-09-05 00:16:14
【问题描述】:

我有一个 ionic 项目,它使用 ion-segment 组件在数据之间切换。数据从周一到周日进行分类。我想使用 getDay 函数自动选择是哪一天,并显示相关的细分选项卡及其数据。

到目前为止我的代码如下...

home.html

<ion-segment class="segment" color="myYellow" [(ngModel)]="days">
    <ion-segment-button value="monday">
      Mon
    </ion-segment-button>
    <ion-segment-button value="tuesday">
      Tue
    </ion-segment-button>
    <ion-segment-button value="wednesday">
      Wed
    </ion-segment-button>
    <ion-segment-button value="thursday">
      Thu
    </ion-segment-button>
    <ion-segment-button value="friday">
      Fri
    </ion-segment-button>
    <ion-segment-button value="saturday">
      Sat
    </ion-segment-button>
    <ion-segment-button value="sunday">
      Sun
    </ion-segment-button>
  </ion-segment>

<div [ngSwitch]="days">
      <ion-list no-lines *ngSwitchCase="'monday'">
          <ion-item *ngFor="let show of shows">
              <ion-thumbnail item-start>
                <img [src]="show.picture.medium">
              </ion-thumbnail>
              <h4 class="title">{{show.show}}</h4>
              <h6 class="host">{{show.hosts}}</h6>
              <p class="time">{{show.time}}</p>
              <button ion-button clear item-end (click)="showInfo($event, show)">View</button>
            </ion-item>
      </ion-list>

      <ion-list no-lines *ngSwitchCase="'tuesday'">
          <ion-item *ngFor="let show of shows">
              <ion-thumbnail item-start>
                <img [src]="show.picture.medium">
              </ion-thumbnail>
              <h4 class="title">{{show.show}}</h4>
              <h6 class="host">{{show.hosts}}</h6>
              <p class="time">{{show.time}}</p>
              <button ion-button clear item-end (click)="showInfo($event, show)">View</button>
            </ion-item>
      </ion-list>
  </div>

然后我的 home.ts 文件将天数定义为字符串,如下所示:

天:字符串 = “星期一”

我的代码运行良好,我不得不手动选择细分中的日期,但我希望它根据您打开应用程序/页面时的星期几自动选择细分中的日期。

【问题讨论】:

    标签: ionic-framework ionic2 ionic3 getdate


    【解决方案1】:

    只需将其用作docs 说:

    假设您有一个public days: string; 变量,该变量在您的模板中用于ngSwitch="days"

    在您的constructor() 正文中:

    let currentDate = new Date();
    let weekdays = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];
    this.days = weekdays[currentDate.getDay()];
    

    【讨论】:

      猜你喜欢
      • 2018-02-17
      • 2017-04-19
      • 1970-01-01
      • 2017-07-10
      • 1970-01-01
      • 2018-11-22
      • 2018-03-31
      • 2019-12-10
      • 2018-04-10
      相关资源
      最近更新 更多