【发布时间】:2018-05-08 05:34:54
【问题描述】:
我正在尝试让 UPDATE 从我的 CRUD API 中工作。我知道问题出在 UPDATE 的表单或组件中,但我不确定我缺少什么。
表单 HTML
<form #form="ngForm" (ngSubmit)="updateReminder(form.value)">
<div class="form-group">
<label for="nameField">Name:</label>
<input name="nameField" class="form-control" [ngModel]="reminders.name">
<br />
<label for="occasionField">Occasion:</label>
<input name="occasionField" class="form-control"
[ngModel]="reminders.occasion">
</div>
<button type="submit">save</button>
</form>
表单组件
import { Component, OnInit, Input } from '@angular/core';
import { ReminderService } from '../providers/reminder.service';
@Component({
selector: 'app-results',
templateUrl: './results.component.html',
styleUrls: ['./results.component.css'],
providers: [ReminderService]
})
export class ResultsComponent implements OnInit {
constructor(private reminderService:ReminderService) {}
reminderList = null;
@Input() reminders;
ngOnInit() {}
updateReminder(obj:any):void {
this.reminders.name = obj.nameField;
this.reminders.occasion = obj.occasionField;
this.reminderService.updateReminder(this.reminders._id, this.reminders)
.subscribe((result) =>{
location.reload();
})
}
应用组件 HTML
<div class="container">
<app-intro title="{{title}}"></app-intro>
<app-form ></app-form>
<app-results *ngFor='let reminder of reminderList' [reminders]="reminder">
</app-results>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256- FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous">
应用组件(没有路由器)
import { Component } from '@angular/core';
import { ReminderService } from './providers/reminder.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: [ReminderService]
})
export class AppComponent {
title = 'The Amazing Reminder App';
constructor(private reminderService:ReminderService) {}
reminderList = null;
ngOnInit() {
this.reminderService.listReminders().subscribe((reminders)=>{
this.reminderList = reminders;
});
}
}
错误在 HTML 中:
错误类型错误:无法读取未定义的属性“名称”
<input name="nameField" class="form-control" [ngModel]="reminders.name">
【问题讨论】:
-
错误发生在哪一行?
-
抱歉,在 HTML 中。 ERROR TypeError: Cannot read property 'name' of undefined
标签: angular