角类型转换如何工作?

时间:2019-05-01 16:07:10

标签: angular typescript

我在我的测试角度应用程序中遇到一些类型的麻烦。我用以下方法写了休息服务:

@PutMapping("/notes")
    public Note updateNote(@RequestBody Note note) {
        System.out.println("It works");
        return service.updateNote(note);
    }

我写了角度服务:

updateNote(note: Note): Observable<any> {
    console.log("Service Updatenote " + typeof note);
    return this.http.put(this.serverURL, note, httpOptions);
  }

我写了角分量:

import { Component, OnInit } from '@angular/core';
import {NotesDataService} from '../../services/notes-data.service';
import {ActivatedRoute} from '@angular/router';
import {Note} from '../../models/note';

@Component({
  selector: 'app-update-note',
  templateUrl: './update-note.component.html',
  styleUrls: ['./update-note.component.css']
})
export class UpdateNoteComponent implements OnInit {

  // notedata: FormGroup;
  // noteId;
  currentNote: Note;
  submited: boolean = false;

  constructor(private dataservise: NotesDataService, private activatedRoute: ActivatedRoute) {
  }

  ngOnInit() {
    const noteId = +this.activatedRoute.snapshot.params['id'];
    this.dataservise.getNoteById(noteId).subscribe(data => {
      console.log("Data ng init " + data);
      this.currentNote = data });
  }

  onClickSubmit() {
    console.log(this.currentNote);
    this.dataservise.updateNote(this.currentNote);
  }
}

并写下了表格:

<div [hidden]="submited">
<form #noteUpdateForm (ngSubmit)="onClickSubmit()">
  <div class="formGroup">
    <label for="title">Title</label>
    <input type="text" class="form-control" id="title" required
           [(ngModel)]="currentNote.title" name="title" #title="ngModel">
    <div [hidden]="title.valid || title.pristine"
         class="alert alert-danger">
      Title is required
    </div>
  </div>
  <label for="body">Body</label>
  <input type="text" class="form-control" id="body" required
         [(ngModel)]="currentNote.body" name="body" #body="ngModel">
  <div [hidden]="body.valid || body.pristine"
       class="alert alert-danger">
    Body is required
  </div>
  <button type="submit">Update</button>
</form>
</div>

请告诉我为什么它不起作用?哪里“ currentNote”的类型更改为对象,或者我做错了什么?

0 个答案:

没有答案