getDay() Ionic method in and out of the constructor, error! - Typescript

Asked

Viewed 282 times

0

Good afternoon guys, this is my first post here on stackoverflow so I already apologize if there are any mistakes..

My question is the following: I am working with Ionic and I need to "take" the day of the week and turn it into a number to work with conditionals for this value, for this there is the getDay() method that returns a value from 0 to 6.. until then ok! When I put inside the constructor as the print below:

The function returns the value normally!!! No errors while running

Now, when I try to create a function outside the constructor, calling the getDay() method, so:

In the html part I put (click)="refresh()" in a button to pass the value returned by getDay to the day variable, gives an error saying that "getDay()" is not a Function. 'Cause in the builder it works, and out it doesn’t?

@Edit code . ts:

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { DatePicker } from '@ionic-native/date-picker';

/**
 * Generated class for the AdicionaPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@Component({
  selector: 'page-adiciona',
  templateUrl: 'adiciona.html',
})
export class AdicionaPage {
  
  today = new Date();
  semana = ["Dom", "Seg", "Ter", "Qua", "Qui", "Sex", "Sáb"]
  listaDia = Array;
  listaMes = Array;
  dia;
  mes;
  
  constructor(public navCtrl: NavController, public navParams: NavParams) {
  
}

  ionViewDidLoad() {
    console.log('ionViewDidLoad AdicionaPage');
  }


  refresh(){
    this.dia = this.semana[this.today.getDate()];
  }



}
<!--
  Generated template for the AdicionaPage page.

  See http://ionicframework.com/docs/components/#navigation for more info on
  Ionic pages and navigation.
-->
<ion-header>

  <ion-navbar>
    <ion-title>Adiciona</ion-title>
  </ion-navbar>

</ion-header>


<ion-content padding>

    <ion-list>
        <h2>Paciente</h2>  
    
          <ion-item>
            <ion-input type="text" placeholder="Nome"></ion-input>
          </ion-item>
        <button clear ion-button>
          <ion-item>
              <ion-datetime placeholder="Data" displayFormat="DD MMM de YYYY" min="2018" [(ngModel)]="today"></ion-datetime>
          </ion-item>
          {{dia}}
        </button>
      </ion-list>
      <button ion-button large color="secondary" class="botao" (click)="refresh()">
        Salvar
      </button>


</ion-content>

  • Put the code instead of image, so it gets easier and fast help-Ló. Read: Post code as image.

  • I do not know where is really your problem, I took the test here and it works normally. see in stackblitz.with ... See your method is this.dia = this.semana[this.today.getDate()]; when it was supposed to be this.dia = this.semana[this.today.getDay()];

  • So wmsouza.. I was trying with getDay and the error was the same! I switched to getDate just in order to test

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.