[英]I am not able to access the id inside both objects
I am new to angular, I am not able to post the values of the id inside Position and Department, for some reason, it is not accessing the id我是 angular 的新手,我无法在 Position 和部门内发布 id 的值,由于某种原因,它没有访问 id
addEmployee.model.ts addEmployee.model.ts
export class AddEmployee {
Position: {
id: string;
};
Department: {
id: string;
};
}
AddEmployeeService添加员工服务
DefineEmployee(addEmployee: AddEmployee) {
const body: AddEmployee = {
Position: {
id: addEmployee.Position.id
},
Department: {
id: addEmployee.Department.id
}
}
return this.http.post('url', body);
}
AddEmployee.component.ts AddEmployee.component.ts
onSubmit(form: NgForm) {
return this.restService.DefineEmployee(form.value).subscribe((res: any) => {
this.resetForm(form);
console.log(res);
}, error => {
this.resetForm(form);
})
}
addEmployee.component.html addEmployee.component.html
<div class="form-group">
//AddEmployee.Position.id comes up with undefined element 'id'
<select [(ngModel)]="AddEmployee.Position.id" name="Position" Position="ngModel" class="form-
control customized-dropdown">
<option [value]="item.id" *ngFor="let item of positions">{{item.name_FL}}</option>
</select>
</div>
as the error is coming from the service AddEmployeeService
, I believe the call to the service is not passing the parameters your function is expecting.由于错误来自服务
AddEmployeeService
,我相信对服务的调用没有传递您的 function 期望的参数。
When you call this.restService.DefineEmployee(form.value)
, I believe the value
property does not have a Position
or Department
property.当您调用
this.restService.DefineEmployee(form.value)
时,我相信value
属性没有Position
或Department
属性。
you can test that by adding a debugger as indicated:您可以通过添加调试器来测试它:
DefineEmployee(addEmployee: AddEmployee) {
console.log(addEmployee);
debugger;
const body: AddEmployee = {
Position: {
id: addEmployee.Position.id
},
...
that should help you fix the function这应该可以帮助您修复 function
The Op posted the contents of the addEmployee
varible as: Op 将
addEmployee
变量的内容发布为:
{
"Position":"c8a06de6-58e1-439a6fc-08d7553139ac",
"Department":"552ccb91-02f8-476b-adbc-08d7553136bf"
}
it seems the ids of Positions and Departments do not have an 'id' property in the "addEmployee" variable;似乎职位和部门的 id 在“addEmployee”变量中没有“id”属性;
so to fix the issue, just remove the 'ids' form the addEmployee
in the service所以要解决这个问题,只需从服务中的
addEmployee
中删除“ids”
DefineEmployee(addEmployee: AddEmployee) {
const body: AddEmployee = {
Position: {
id: addEmployee.Position // <<< here
},
Department: {
id: addEmployee.Department // <<< here
}
}
return this.http.post('url', body);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.