我正在创建一个简单的表单来提交数据,电子邮件,城市名称和酒店名称,当我单击“提交”时出现此错误:
AboutComponent.html:79错误TypeError:无法读取属性“值” 的未定义
这是我所拥有的: comment.js
// Comments Schema
const CommentsSchema = mongoose.Schema({
email: {
type: String,
required: true
},
name: {
type: String,
required: true
}
},{
collection: 'comments'
});
const Comments = module.exports = mongoose.model('Comments', CommentsSchema);
component.ts
addReview(email, name) {
this.moviesService.addReview(email, name).subscribe(success => {
this.flashMessages.show('You are data we succesfully submitted', { cssClass: 'alert-success', timeout: 3000 });
// get the id
this.activeRouter.params.subscribe((params) => {
// tslint:disable-next-line:prefer-const
let id = params['id'];
this.moviesService.getComments(id)
.subscribe(comments => {
console.log(comments);
this.comments = comments;
});
});
}, error => {
this.flashMessages.show('Something went wrong', { cssClass: 'alert-danger', timeout: 3000 });
});
}
html
<form [formGroup]="angForm" class="form-element">
<div class="form-group form-element_email">
<input type="email" class="form-control info" placeholder="Email" formControlName="email" #email (ngModelChange)="onChange($event)">
</div>
<div *ngIf="angForm.controls['email'].invalid && (angForm.controls['email'].dirty || angForm.controls['email'].touched)"
class="alert alert-danger">
<div *ngIf="angForm.controls['email'].errors.required">
Email is required.
</div>
</div>
<div class="input-group mb-3 form-element_city">
<select class="custom-select" id="inputGroupSelect01">
<option selected *ngFor="let city of cities" [ngValue]="city.name" #name>{{city.name}}</option>
</select>
</div>
<div class="input-group mb-3 form-element_hotel">
<select class="custom-select" id="inputGroupSelect01">
<option selected *ngFor="let hotel of hotels" [ngValue]="hotel.name" #name>{{hotel.name}}</option>
</select>
</div>
<div class="form-group">
<button type="submit" (click)="addReview(email.value, name.value)" class="btn btn-primary btn-block form-element_btn" [disabled]="!validEmail">ACCEDER</button>
</div>
</form>
service.ts
addReview(email, name): Observable<any> {
const uri = `${apiUrl + this.addCommentsUrl}`;
const obj = {
email: email,
name: name
};
return this.http.post(uri, obj);
}
注意:当我删除姓名时,一切正常,但是我还需要将城市名称和酒店名称与电子邮件一起插入到我的数据库中。
我的代码有什么问题?
答案 0 :(得分:1)
我认为templateRef var应该在select元素上,而不是option元素上。
相反
<option selected *ngFor="let city of cities" [ngValue]="city.name" #name>{{city.name}}</option>
您能尝试一下吗?
<select class="custom-select" id="inputGroupSelect01">
在旁注templateRef变量名称对于每个选择元素应该唯一。您不能对两个不同的元素使用相同的变量。
在此处查看#name:
1. <option selected *ngFor="let city of cities" [ngValue]="city.name" #name>{{city.name}}</option>
2. <option selected *ngFor="let hotel of hotels" [ngValue]="hotel.name" #name>{{hotel.name}}</option>
如果有帮助,请告诉我!