提交具有单个字段的表单时,自动更新时间戳记的角度

问题描述 投票:0回答:2

当我创建/编辑便笺时,日期未添加。在提交表单并将生成的时间戳推入对象并保存诸如“ createdOn”之类的数据时,有什么方法可以自动更新日期:“ 2019-07-15T01 :52:33Z“; modifiedOn:” 2019-07-15T01:52:33Z“

model.ts

导出类注释{构造函数公开名称:string,public createdOn:Date,公开修改日期:){}}

。html

<div class="container">
  <div class="row">
    <form [formGroup]="noteEditForm" (submit)="onSubmit()">

      <div class="row">
        <div class="form-group p-3">
          <label for="name">Note Text :</label>
          <textarea  class="form-control" formControlName="name" cols="100" rows="10"></textarea>
        </div>
      </div>

      <div class="row justify-content-end">
        <button class="btn btn-success mx-2" type="submit">Save &#10003;</button>
        <button class="btn btn-danger" type="button" (click)="onCancel()">Cancel &#10005;</button>
      </div>

    </form>
  </div>
</div>

。ts文件

如何在此.ts文件中将时间戳记推送到这里

export class NoteEditComponent implements OnInit {
  public notes:Notes
  public editMode: boolean = false;
  public id: number;

  noteEditForm: FormGroup;

  constructor(private noteService:NotesService, private route:ActivatedRoute, private router:Router, private dataService:DataService) { }

  ngOnInit() {
    this.route.params.subscribe((params: Params) => {
      this.id = +params['id']
      this.editMode = params['id'] != null
      this.initializeForm()
    })
  }

  initializeForm(){
    let noteName = '';
    if(this.editMode){
      const note = this.noteService.getNoteById(this.id)
      noteName = note.name      
      console.log(note)
    }
    this.noteEditForm = new FormGroup({
      'name': new FormControl(noteName)
    })
  }

  onSubmit(){
    if(this.editMode){
      this.noteService.editNote(this.id,this.noteEditForm.value)

    }else{
      this.noteService.addNewNote(this.noteEditForm.value)
    }
    this.onCancel()
  }

  onCancel(){
    this.router.navigate(['../'],{relativeTo:this.route})
  }

}

。service.ts

export class NotesService {

  public notesChangeInDOM = new Subject<Notes[]>();

  private notes: Notes[] = [
    new Notes(
      'This Notes is related to the thing Dummy Content',
      new Date(),
      new Date()
    ),
    new Notes(
      'The time that sun rises and sets is the most beautiful scene',
      new Date(),
      new Date()
    ),
    new Notes(
      'The documents has to be uploaded to the cliets before the deadline',
      new Date(),
      new Date()
    ),
    new Notes(
      'Meeting has to be scheduled by this week',
      new Date(),
      new Date()
    ),
  ];


  constructor() { }


  setNotes(note:Notes[]){
    this.notes = note
  }

  getAllNotes(){
     return this.notes.slice()
  }

  getNoteById(id:number){
    return this.notes[id]
  }

  addNewNote(note:Notes){
    this.notes.push(note)
    return this.notesChangeInDOM.next(this.notes.slice())
  }

  editNote(id:number, note:Notes){
    this.notes[id] = note
    return this.notesChangeInDOM.next(this.notes.slice())   
  }

  deleteNote(id:number){
    this.notes.splice(id,1)
    return this.notesChangeInDOM.next(this.notes.slice())
  }

}
angular firebase-realtime-database angular7
2个回答
0
投票

您可以在组件类中更新onSubmit函数,以将日期添加到Notes对象。


0
投票

您可以在服务方法中通过编辑时更新修改日期来处理此问题。

© www.soinside.com 2019 - 2024. All rights reserved.