0

我有:
carico.model.ts

export class Carico {
  $key: string;
  dataCarico:  Date;
  riferimentoCarico: string;
  dettaglio:{
    carburante: string;
    quantita: string;
  }  
}

在我的服务中:
carichi.service.ts

import { Injectable } from '@angular/core';
....some code...    
import { Carico } from './carico.model';

@Injectable()
export class CarichiService {
  ...some code...    

  getCarichiList(query = {}): FirebaseListObservable<Carico[]> {
    if (!this.userId) return;
    this.carichi = this.db.list(`carico/${this.userId}`, { query: query }
    );
    return this.carichi
  }    
  ...some code...

  createCarico(carico: Carico): void {
    this.carichi.push(carico)
      .catch(error => this.handleError(error))
  }    
}

在我的表单组件中:
new-carico-form.component.ts

import { Carburante } from '../../impostazioni/carburanti/carburante.model';
import { CarburantiService } from '../../impostazioni/carburanti/carburanti.service';
...some code...

constructor(...) {    
    this.createForm();
  }

  ngOnInit() {
    this.carburanti = this.carburantiService.getCarburantiList();
  }

  createForm() {
    this.caricoForm = this.fb.group({
      dataCarico: Date,
      riferimentoCarico: [''],
      dettaglio: this.fb.group({        
        carburante: '',
        quantita: ''
      })
    });
  }

 inserisciCarico(carico: Carico) {
    this.carichiService.getCarichiList();       
    this.carichiService.createCarico(this.caricoForm.value);    
    this.caricoForm.reset();
  }

  onSubmit() {
  }

}

和我的 html:
new-carico-form-component.html

<md-card>  
    <form [formGroup]="caricoForm" novalidate>
      <md-card-content>
        <md-input-container >
          <input mdInput [mdDatepicker]="picker" placeholder="seleziona data" formControlName="dataCarico">
          <button mdSuffix [mdDatepickerToggle]="picker" ></button>
        </md-input-container>
        <md-datepicker #picker></md-datepicker>
        <span class="col"> </span>
        <md-input-container>
          <input mdInput placeholder="riferimento" formControlName="riferimentoCarico">
        </md-input-container>
        <table formGroupName="dettaglio">          
          <tr>
            <td>
              <md-select placeholder="carburante" formControlName="carburante">                    
...some code...

问题是我可以保存表单唯一不起作用的是日期。我尝试在订阅前后显示在控制台中,日期在表格中。有什么建议么?我回到 Firebase,除了日期之外什么都没有。

4

1 回答 1

0

您将无法Date对象推送到 firebase 数据库。您需要使用Date.prototype.toISOString()Date.prototype.getTime() 之Date类的东西将其转换为毫秒或字符串

inserisciCarico(carico: Carico) {
   this.carichiService.getCarichiList();
   let formValue = this.caricoForm.value;
   formValue.dataCarico = formValue.dataCarico.getTime();
   // or
   // formValue.dataCarico = formValue.dataCarico.getISOString(); 

   this.carichiService.createCarico(formValue );    
   this.caricoForm.reset();
}

如果您检查dateCaricousing的类型,Object.prototype.toString.call(carico.dateCarico)您会看到它[object Date]来自mdDatepicker,这需要转换为毫秒或字符串才能进入 firebase 数据库。

您可能需要修改Carico类以接受日期或字符串:

dataCarico:  Date | string;

希望有帮助!

于 2017-08-08T14:58:55.430 回答