I'm currently working on a project using Ionic and Angular, and I'm facing the challenge of creating a select dropdown with a specific range of numbers. However, manually inputting these numbers is not efficient, so I'm looking for a way to generate them dynamically in TypeScript.
In the "ages" variable, I aim to have the numbers generated as selectable options for the dropdown.
Here is the TypeScript file code snippet:
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
@Component({
selector: 'app-user-appearance',
templateUrl: './user-appearance.component.html',
styleUrls: ['./user-appearance.component.scss']
})
export class UserAppearanceComponent {
appearanceForm = new FormGroup({
age: new FormControl(''),
height: new FormControl(''),
weight: new FormControl(''),
ethnicity: new FormControl(''),
eyesColor: new FormControl(''),
hairLength: new FormControl(''),
hairColor: new FormControl('')
});
ages: number[] = [1,2,3,4,5,6,7,8,9,10];
constructor() { }
submit() {
console.log(this.appearanceForm.get('age').value);
}
}
And here is the corresponding HTML code:
<ion-header>
<ion-toolbar>
<ion-buttons slot="start">
<ion-back-button text="{{'back' | translate}}"></ion-back-button>
</ion-buttons>
</ion-toolbar>
</ion-header>
<ion-content class="ion-padding">
<h1>{{'letUsKnowHowYouLook' | translate}}</h1>
<h3>{{'easyToIdentifyYou' | translate}}</h3>
<form [formGroup]="appearanceForm" (ngSubmit)="submit()">
<ion-item >
<ion-label>{{'age' | translate}}</ion-label>
<ion-select placeholder='{{"selectOne" | translate}}' formControlName="age">
<ion-select-option *ngFor="let age of ages">{{age}}</ion-select-option>
</ion-select>
</ion-item>
<app-input [label]="'height'| translate" formControlName="height"></app-input>
<app-input [label]="'weight'| translate" formControlName="weight"></app-input>
<app-input [label]="'ethnicity'| translate" formControlName="ethnicity"></app-input>
<app-input [label]="'eyesColor'| translate" formControlName="eyesColor"></app-input>
<app-input [label]="'hairLength'| translate" formControlName="hairLength"></app-input>
<app-input [label]="'hairColor'| translate" formControlName="hairColor"></app-input>
<ion-button expand="block" type="submit" >{{'confirm' | translate}}</ion-button>
</form>
</ion-content>