角度6所形成,取对象到输入(Angular 6 forms, fetch object into i

2019-11-05 05:05发布

有人可以帮我与data.name到输入获取的对象,这是网站形式输入的画面

这是部件的HTML的代码

 <ng-template #content let-modal> <div class="modal-header"> <h4 class="modal-title">Edit Movie</h4> <button type="button" class="close" aria-label="Close" (click)="modal.dismiss('Cross click')"> <span aria-hidden="true">&times;</span> </button> </div> <div class="modal-body"> <form #editMovieForm="ngForm" (ngSubmit)="onSave(editMovieForm)"> <div class="form-group"> <label for="runTime">Run Time</label> <input type="text" name="runTime" id="runTime" class="form-control" [(ngModel)]="movie.runtime" > </div> <div class="form-group"> <label for="genre">Genre</label> <input type="text" name="genre" id="genre" class="form-control" [(ngModel)]="movie.genres" value="" > </div> <div class="form-group"> <label for="cast">Cast</label> <input type="text" name="cast" id="cast" class="form-control" [(ngModel)]="movie.cast" > </div> <input type="submit" class="btn btn-success" value="Save"> <button type="button" class="btn btn-danger float-right" (click)="onDelete()" ><i class="far fa-trash-alt"></i> Remove</button> </form> </div> </ng-template> <button class="btn btn-light mb-2 mr-2" (click)="openBackDropCustomClass(content)">Edit Movie</button> 

这是一个分量

 export class EditMovieModalComponent implements OnInit { closeResult: string; movie: any = new Object(); id: number; constructor( private modalService: NgbModal, private movieService: MovieRequestService, private flashMessage: FlashMessagesService, private http: HttpClient, private router: Router, private route: ActivatedRoute ) { this.getMovieDetails(); } openBackDropCustomClass(content) { this.modalService.open(content, {backdropClass: 'light-blue-backdrop'}); } ngOnInit() { } getMovieDetails () { // Get Id from URL this.id = this.route.snapshot.params['id']; this.movieService.getMovieDetails(this.id).subscribe(response => this.movie = response.data.movie); } onDelete () { if (confirm('Are you sure ???')) { this.movieService.deleteMovie(this.movie); this.flashMessage.show('Movie Removed Succes'); } } onSave () {} } 

我无法获取cast.name的数据,因为它的对象我可怎么办呢? 以及我如何保存编辑电影到角虚拟DOM或东西吗?

Answer 1:

什么是“投”在这里? 它是一个字符串? 它是(例如)firstName和lastName的组合? 然后,它是一个对象。 你需要像指定:movie.cast.firstName



Answer 2:

在角响应会作为观测量,你必须给它正确读取数据的结构,我想你还没有定义为可观察的任何结构(接口或类),所以在这种情况下,我认为地图功能将解决您的问题。 防爆:

getMovieDetails() {
return this.http.get('your URL')
    .map(res => res);
}

我希望这能解决你的问题,如果没有,那么请发表你在哪里做后端调用的代码。

您可以参考此链接理解角HTTP调用和观测量:

角HTTP



文章来源: Angular 6 forms, fetch object into input
标签: angular