对象内部的未定义值

时间:2019-03-19 19:11:23

标签: javascript angular typescript ionic-framework ionic4

我想用服务器中的数据填充页面,并能够更改信息,为此,我正在使用formbuilder来使用以下方法将服务器中的数据作为默认值:

 createForm(){
    this.getGeral()
    this.getTiposTarefas()
    this.formulario = this.fb.group({
      'tipo_tarefa':[this.tarefa.tipoTarefa.id, Validators.compose([Validators.required])], // Cant set default values cuz the array is object is undefined
      'data_tarefa': [this.tarefa.data_tarefa, Validators.compose([Validators.required])],// Cant set default values cuz the array is object is undefined
      'inicio_tarefa': [this.tarefa.inicio, Validators.compose([Validators.required])],// Cant set default values cuz the array is object is undefined
      'fim_tarefa': [this.tarefa.fim, Validators.compose([Validators.required])]// Cant set default values cuz the array is object is undefined
   });
  }

但是我不确定。我尝试在de subscription函数中使用console.log()并填充了对象“ Tarefa”,但不在函数范围之内。

    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)=>{ //passar o id da tarefa como parametro no recupera detalhes
        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])], // Cant set default values cuz the array is object is undefined
      'data_tarefa': [this.tarefa.data_tarefa, Validators.compose([Validators.required])],// Cant set default values cuz the array is object is undefined
      'inicio_tarefa': [this.tarefa.inicio, Validators.compose([Validators.required])],// Cant set default values cuz the array is object is undefined
      'fim_tarefa': [this.tarefa.fim, Validators.compose([Validators.required])]// Cant set default values cuz the array is object is undefined
   });
  }
 closeModal()
 {
   this.modalController.dismiss();
 }
}

运行Ionic服务时,它会向我抛出以下错误,并且我无法在formcontrols上设置默认值:

enter image description here

我的HTML:

<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>

1 个答案:

答案 0 :(得分:1)

删除功能class Parser def parse(text) if text.encoding.name != "UTF-8" encoded_text = @full_diff.encode("UTF-8", "binary", { :invalid => :replace, :undef => :replace }) else encoded_text = text end hunks = [] hunk = nil added_line_number = nil deleted_line_number = nil lines = encoded_text.strip.split("\n") lines.each_with_index do |line, index| if m = /^diff --git a\/(.*?) b\/(.*?)$/.match(line) raise "Diff formatting error, 'diff --git' is the last line" if index + 1 >= lines.length # new hunk added_line_number = nil delete_line_number = nil hunk = Hunk.new(m[1], m[2]) hunk.type = hunk_type(lines[index + 1], m[1], m[2]) hunks.push(hunk) elsif /^Binary files /.match(line) hunk.is_binary = true elsif m = /^@@ \-(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@/.match(line) # (e.g. @@ -19,6 +19,7 @@) deleted_line_number = Integer(m[1]) added_line_number = Integer(m[2]) else if !added_line_number.nil? if line.start_with?('+') # added line hunk.lines.push SourceLine.new(added_line_number, SourceLine::Type::Added, line[1..-1]) added_line_number += 1 elsif line.start_with?('-') # deleted line hunk.lines.push SourceLine.new(deleted_line_number, SourceLine::Type::Deleted, line[1..-1]) deleted_line_number += 1 else # unmodified line added_line_number += 1 deleted_line_number += 1 end end end end hunks end def hunk_type(line, original, renamed) case line when /^new file / type = Hunk::Type::Added when /^deleted file / type = Hunk::Type::Deleted else type = original == renamed ? Hunk::Type::Modified : Hunk::Type::Renamed end type end private :hunk_type end end module Type Added = 'added' Deleted = 'deleted' Modified = 'modified' Renamed = 'renamed' end class Hunk module Type Added = 'added' Deleted = 'deleted' Modified = 'modified' Renamed = 'renamed' end attr_accessor :original_path, :renamed_path, :type, :lines, :is_binary alias_method :is_binary?, :is_binary def initialize(original_path, renamed_path) self.is_binary = false self.lines = [] self.original_path = original_path self.renamed_path = renamed_path end end class SourceLine module Type Added = 'added' Deleted = 'deleted' end attr_accessor :number, :type, :text def initialize(number, type, text) self.number = number self.type = type self.text = text end end ,然后进行以下更改。

getTiposTarefas()

更新: 您需要首先初始化此createForm(){ this.getGeral(); this.getTarefaDetalhe.recuperaTiposTarefas().subscribe((data: TipoTarefa[])=>{ this.tiposTarefas = data; this.formulario = this.fb.group({...put the fields...}); console.log(this.tiposTarefas) // here it has information }) } 对象。见下文

tarefa: Tarefa

然后应该可以正常工作。