0
votes

With Angular-7, I am trying to use select option inside Angular component typescript to generate date of birth with day, month, and year separate.

client.component.ts

import { Component, OnInit, ElementRef, NgZone, ViewChild } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { ApiService } from '../../shared/services/api.service';
import { TokenService } from '../../shared/services/token.service';
import { Router } from '@angular/router';

const getMonth = (idx) => {

  var objDate = new Date();
  objDate.setDate(1);
  objDate.setMonth(idx-1);

  var locale = "en-us",
      month = objDate.toLocaleString(locale, { month: "long" });

    return month;
}


@Component({
  selector: 'app-client-quotes-landing',
  templateUrl: './client-quotes-landing.component.html',
  styleUrls: ['./client-quotes-landing.component.scss']
})
export class ClientQuotesLandingComponent implements OnInit {

  public title = 'Places';
  public addrKeys: string[];
  public addr: object;

  months = Array(12).fill(0).map((i,idx) => getMonth(idx + 1));

  public form = {
    first_name : null,
    last_name : null,
    dob_day : null,
    dob_month : null,
    dob_year : null,
  };

  public error = {
    'first_name' : null,
    'last_name' : null,
    'dob_day' : null,
    'dob_month' : null,
    'dob_year' : null,
  };

  public get days() {
    const dayCount = this.getDaysInMonth(this.form.dob_year, this.form.dob_month);
    return Array(dayCount).fill(0).map((i,idx) => idx +1)
  }

  public getDaysInMonth(year: number, month: number) {
    return 32 - new Date(year, month - 1, 32).getDate();
  }

  constructor(
    private api: ApiService,
    private token: TokenService,
    private router: Router,
    private notify: SnotifyService,
    private zone: NgZone,
    ) {
     }

  ngOnInit() {

  }

}

I want to access dob_month and dob_year from:

public form = {...}

Then, I got this error:

ERROR in src/app/pages/client-quotes-landing/client-quotes-landing.component.ts(94,47): error TS2339: Property 'dob_year' does not exist on type 'ClientQuotesLandingComponent'. src/app/pages/client-quotes-landing/client-quotes-landing.component.ts(94,62): error TS2339: Property 'dob_month' does not exist on type 'ClientQuotesLandingComponent'.

The error came from:

  public get days() {
    const dayCount = this.getDaysInMonth(this.form.dob_year, this.form.dob_month);
    return Array(dayCount).fill(0).map((i,idx) => idx +1)
  }

How do I resolve this error?

1

1 Answers

0
votes

The problem seems to be coming from the function you have declared

Try removing space berween the get and days keyword of the function

Like

public getdays() {
    const dayCount = this.getDaysInMonth(this.form.dob_year, this.form.dob_month);
    return Array(dayCount).fill(0).map((i,idx) => idx +1)
  }