I am looking to populate a page with data from the server and have the ability to update the information. To achieve this, I am using formbuilder to fetch data from the server as the default value. Here's how I am implementing it:
createForm(){
this.getGeral()
this.getTiposTarefas()
this.formulario = this.fb.group({
'tipo_tarefa':[this.tarefa.tipoTarefa.id, Validators.compose([Validators.required])], // Unable to set default values because the array object is undefined
'data_tarefa': [this.tarefa.data_tarefa, Validators.compose([Validators.required])],// Unable to set default values because the array object is undefined
'inicio_tarefa': [this.tarefa.inicio, Validators.compose([Validators.required])],// Unable to set default values because the array object is undefined
'fim_tarefa': [this.tarefa.fim, Validators.compose([Validators.required])]// Unable to set default values because the array object is undefined
});
}
However, the values are coming up as undefined. I inserted console.log() inside the subscribe function and confirmed that the object 'Tarefa' is populated, but it is not outside the function scope.
import { Component, OnInit } from '@angular/core';
import { NavParams, ModalController } from '@ionic/angular';
import { TarefadetalheService } from './tarefadetalhe.service';
import { Tarefa } from '../../models/tarefa.model';
import { TipoTarefa } from '../../models/tipotarefa.model';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
@Component({
selector: 'app-tarefas-detalhe',
templateUrl: './tarefas-detalhe.page.html',
styleUrls: ['./tarefas-detalhe.page.scss'],
})
export class TarefasDetalhePage implements OnInit {
idTarefa = null
tarefa: Tarefa
tiposTarefas : TipoTarefa[]
formulario: FormGroup
constructor(
private navParams: NavParams,
private getTarefaDetalhe: TarefadetalheService,
private modalController:ModalController,
public fb: FormBuilder) { }
ngOnInit() {
this.createForm()
}
getGeral(){
this.idTarefa = this.navParams.get('id_tarefa');
this.getTarefaDetalhe.recuperaDetalhes().subscribe((data: Tarefa)=>{ //pass the task id as a parameter in the recovery details
this.tarefa = data
})
}
getTiposTarefas(){
this.getTarefaDetalhe.recuperaTiposTarefas().subscribe((data: TipoTarefa[])=>{
this.tiposTarefas = data
console.log(this.tiposTarefas) // here it has information
})
console.log(this.tiposTarefas) // here it has not information
}
createForm(){
this.getGeral()
this.getTiposTarefas()
this.formulario = this.fb.group({
'tipo_tarefa':[this.tarefa.tipoTarefa.id, Validators.compose([Validators.required])], // Unable to set default values because the array object is undefined
'data_tarefa': [this.tarefa.data_tarefa, Validators.compose([Validators.required])],// Unable to set default values because the array object is undefined
'inicio_tarefa': [this.tarefa.inicio, Validators.compose([Validators.required])],// Unable to set default values because the array object is undefined
'fim_tarefa': [this.tarefa.fim, Validators.compose([Validators.required])]// Unable to set default values because the array object is undefined
});
}
closeModal()
{
this.modalController.dismiss();
}
}
After running Ionic serve, I encountered the following error, preventing me from setting default values on the form controls:
https://i.sstatic.net/h4tTu.png
Here is the HTML code:
<ion-content padding *ngIf="tarefa != null">
<form [formGroup]="formulario">
<h4>
<ion-icon name="list-box"></ion-icon> Geral
</h4>
<ion-grid>
<ion-row>
<ion-col size="8">
<ion-label position="floating">Tipo de Tarefa</ion-label>
<ion-select [formControlName]="tipo_tarefa" okText="Confirmar" cancelText="Cancelar">
<ion-select-option *ngFor="let tipo of tiposTarefas" [value]="tipo.id">{{tipo.descricao}}</ion-select-option>
</ion-select>
</ion-col>
</ion-row>
</ion-grid>
<h4>
<ion-icon name="calendar"></ion-icon> Horário
</h4>
<ion-item-divider></ion-item-divider>
<ion-grid>
<ion-row>
<ion-col size="5">
<ion-label position="stacked">Data</ion-label>
<ion-datetime [formControlName]="data_tarefa" display-format="DD-MM-YYYY" max="2050-10-31" picker-format="DD-MM-YYYY"></ion-datetime>
</ion-col>
<ion-col size="3">
<ion-label position="stacked">Inicio</ion-label>
<ion-datetime [formControlName]="inicio_tarefa" display-format="HH:mm" picker-format="HH:mm" ></ion-datetime>
</ion-col>
<ion-col size="3">
<ion-label position="stacked">Fim</ion-label>
<ion-datetime [formControlName]="fim_tarefa" display-format="HH:mm" picker-format="HH:mm"></ion-datetime>
</ion-col>
</ion-row>
</ion-grid>
<h4>
<ion-icon name="person"></ion-icon> Cliente
</h4>
<ion-item-divider></ion-item-divider>
<ion-grid>
<ion-row>
</ion-row>
</ion-grid>
</form>
</ion-content>