Javascript 无法设置属性';地点';未定义的
我有一个add表单,在其中输入位置和其他数据。 在service.ts中我已声明Javascript 无法设置属性';地点';未定义的,javascript,angular,Javascript,Angular,我有一个add表单,在其中输入位置和其他数据。 在service.ts中我已声明 export class SBOrderViewModel { location: string; agent: string; } 在中添加.component.ts sboVM: SBOrderViewModel; this.addForm = this.formBuilder.group({ location: [], agent: [], }); onSubmit
export class SBOrderViewModel {
location: string;
agent: string;
}
在中添加.component.ts
sboVM: SBOrderViewModel;
this.addForm = this.formBuilder.group({
location: [],
agent: [],
});
onSubmit() {
this.sboVM.location = this.addForm.value.location;
this.sboVM.agent = this.addForm.value.agent;
this.sboService.Insert(this.sboVM).subscribe(
data => {
this.router.navigate(['/sborder/']);
},
this.errHandler.bind(this)
);
}
无论何时从前端提交数据,它都会转到add.componet.ts中的onSubmit()函数
sboVM: SBOrderViewModel;
this.addForm = this.formBuilder.group({
location: [],
agent: [],
});
onSubmit() {
this.sboVM.location = this.addForm.value.location;
this.sboVM.agent = this.addForm.value.agent;
this.sboService.Insert(this.sboVM).subscribe(
data => {
this.router.navigate(['/sborder/']);
},
this.errHandler.bind(this)
);
}
在add.componet.html中
<form #addorder [formGroup]="addForm" clrForm clrLayout="horizontal">
<clr-input-container>
<label>Location</label>
<input class="txtboxwidth" formControlName="location" clrInput type="text" name="location"/>
</clr-input-container>
<button class="mrgnlft btn btn-success" [disabled]='addForm.invalid' *ngIf="btnvisibility" type="submit" (click)="onSubmit()">Save</button>
</form>
位置
拯救
我不明白为什么在addform和submit中添加值时显示无法设置未定义的属性'location'。非常感谢您的帮助您可以通过更新add.component.ts中的以下代码进行检查
this.addForm = new FormGroup({
location: new FormControl(null, [])
agent: new FormControl(null, [])
});
您可以通过更新add.component.ts中的以下代码进行检查
this.addForm = new FormGroup({
location: new FormControl(null, [])
agent: new FormControl(null, [])
});
似乎这个.sboVM在onSubmit函数中为空。 您的代码正在运行的其他bcs
看起来像这个.sboVM在onSubmit函数中为空。 您的代码正在运行的其他bcs
您必须初始化变量this.sboVM
在构造函数中添加:
this.sboVM=new SBOrderViewModel()代码>您必须初始化变量this.sboVM
在构造函数中添加:this.sboVM=new SBOrderViewModel()代码>@Ale\u Binaco感谢您的友好回复。我在构造函数中添加了this.sboVM=new SBOrderViewModel();但现在它显示,当我提交form@Mahmud当我请求/api/sborder/insert it http response 400错误并显示jason值无法转换为system.int 32时,您可以通过检查表单是否为null@Ale_Binaco来包装表单。路径:$orderId@Mahmud我认为这可能是一个api反序列化问题。如果您使用的是ASP.NET Core 3.0,则默认包为System.Text.Json。您可以使用Newtonsoft(安装Microsoft.AspNetCore.Mvc.NewtonsoftJson和在startup services.AddControllers().AddNewtonsoftJson();)@Ale_Binaco感谢您的回复。我在构造函数中添加了this.sboVM=new SBOrderViewModel();但现在它显示,当我提交form@Mahmud当我请求/api/sborder/insert it http response 400错误并显示jason值无法转换为system.int 32时,您可以通过检查表单是否为null@Ale_Binaco来包装表单。路径:$orderId@Mahmud我认为这可能是一个api反序列化问题。如果您使用的是ASP.NET Core 3.0,则默认包为System.Text.Json。您可以使用Newtonsoft(安装Microsoft.AspNetCore.Mvc.NewtonsoftJson和在startup services.AddControllers().AddNewtonsoftJson();)感谢您的友好响应。是的,此.sboVM为空。在构造函数内声明this.sboVM=new SBOrderViewModel()后,它被解析。但是当我提交表单时,它显示无法读取null的属性“control”。感谢您的友好响应。是的,this.sboVM为null。在构造函数内声明this.sboVM=new SBOrderViewModel()后,它被解析。但当我提交表单时,它显示无法读取null的属性“control”。