ERROR TypeError: Cannot read property '_id' of undefined at Object.eval [as updateDirectives]
ERROR TypeError: Cannot read property '_id' of undefined at Object.eval [as updateDirectives]
我在数据绑定 _id 属性 后收到此错误。谁能帮帮我。
代码:
<div class="form-group">
<input type="hidden" name="_id" #_id="ngModel" [(ngModel)]="iphoneService.selectedIphone._id">
</div>
这是iphone.Service代码
import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { Observable } from "rxjs/Observable";
import "rxjs/add/operator/map";
import "rxjs/add/operator/toPromise";
import { Iphone } from "./iphone.model";
@Injectable()
export class IphoneService {
selectedIphone: Iphone;
iphones: Iphone[];
readonly baseUrl = "http://localhost:3000/iphone";
constructor(private http: HttpClient) {}
postIphone(iphon: Iphone) {
return this.http.post(this.baseUrl, iphon);
}
}
这是iphon.model
export interface Iphone {
_id: string;
firstName: string;
lastName: string;
email: string;
colorPrefence: string;
contact: string;
country: string;
state: string;
city: string;
zipCode: string;
transactionId: string;
__v: string;
}
这里是iphone.component.ts
@Component({
selector: "app-iphone",
templateUrl: "./iphone.component.html",
styleUrls: ["./iphone.component.css"],
providers: [IphoneService]
})
export class IphoneComponent implements OnInit, AfterViewChecked {
constructor(public iphoneService: IphoneService) {}
ngOnInit() {}
onSubmit(form: NgForm) {
this.iphoneService.postIphone(form.value).subscribe(res => {});
}
我认为我遇到的错误是由于模型绑定不正确,但我不知道如何解决它。
您在制定工作流程时遇到了几个问题。
首先,您尝试绑定到存在于服务中而非组件中的变量。服务不应该负责存储视图状态。视图状态应该是组件的责任。您需要在您的组件上有一个变量(例如 selectedIphoneId
),然后您可以在 [(ngModel)]
中使用它。
同时删除模板引用的分配 #_id=“ngModel
。如果您甚至不需要使用它,请摆脱它。你会得到这样的东西:
<div class="form-group">
<input type="hidden" name="_id" #_id [(ngModel)]="selectedIphoneId">
</div>
我在数据绑定 _id 属性 后收到此错误。谁能帮帮我。
代码:
<div class="form-group">
<input type="hidden" name="_id" #_id="ngModel" [(ngModel)]="iphoneService.selectedIphone._id">
</div>
这是iphone.Service代码
import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { Observable } from "rxjs/Observable";
import "rxjs/add/operator/map";
import "rxjs/add/operator/toPromise";
import { Iphone } from "./iphone.model";
@Injectable()
export class IphoneService {
selectedIphone: Iphone;
iphones: Iphone[];
readonly baseUrl = "http://localhost:3000/iphone";
constructor(private http: HttpClient) {}
postIphone(iphon: Iphone) {
return this.http.post(this.baseUrl, iphon);
}
}
这是iphon.model
export interface Iphone {
_id: string;
firstName: string;
lastName: string;
email: string;
colorPrefence: string;
contact: string;
country: string;
state: string;
city: string;
zipCode: string;
transactionId: string;
__v: string;
}
这里是iphone.component.ts
@Component({
selector: "app-iphone",
templateUrl: "./iphone.component.html",
styleUrls: ["./iphone.component.css"],
providers: [IphoneService]
})
export class IphoneComponent implements OnInit, AfterViewChecked {
constructor(public iphoneService: IphoneService) {}
ngOnInit() {}
onSubmit(form: NgForm) {
this.iphoneService.postIphone(form.value).subscribe(res => {});
}
我认为我遇到的错误是由于模型绑定不正确,但我不知道如何解决它。
您在制定工作流程时遇到了几个问题。
首先,您尝试绑定到存在于服务中而非组件中的变量。服务不应该负责存储视图状态。视图状态应该是组件的责任。您需要在您的组件上有一个变量(例如 selectedIphoneId
),然后您可以在 [(ngModel)]
中使用它。
同时删除模板引用的分配 #_id=“ngModel
。如果您甚至不需要使用它,请摆脱它。你会得到这样的东西:
<div class="form-group">
<input type="hidden" name="_id" #_id [(ngModel)]="selectedIphoneId">
</div>