Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我无法访问两个对象内的id_Javascript_Html_Angular_Typescript - Fatal编程技术网

Javascript 我无法访问两个对象内的id

Javascript 我无法访问两个对象内的id,javascript,html,angular,typescript,Javascript,Html,Angular,Typescript,我是angular的新手,我无法在职位和部门内发布id的值,因为某些原因,它无法访问id addEmployee.model.ts export class AddEmployee { Position: { id: string; }; Department: { id: string; }; } AddEmployeeService DefineEmployee(addEmployee: AddEmployee) { const body: AddEm

我是angular的新手,我无法在职位和部门内发布id的值,因为某些原因,它无法访问id

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

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

<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>

//AddEmployee.Position.id出现未定义的元素“id”
{{item.name_FL}}

由于错误来自服务
AddEmployeeService
,我相信对该服务的调用没有传递函数期望的参数

当您调用
this.restService.DefineEmployee(form.value)
时,我相信
value
属性没有
Position
Department
属性

您可以通过添加调试器进行测试,如下所示:

DefineEmployee(addEmployee: AddEmployee) {
  console.log(addEmployee);
  debugger;
  const body: AddEmployee = {

    Position: {
      id: addEmployee.Position.id
    },
    ...
这将有助于修复函数

更新 Op将添加员工的内容发布为:

{
 "Position":"c8a06de6-58e1-439a6fc-08d7553139ac",
 "Department":"552ccb91-02f8-476b-adbc-08d7553136bf"
}
似乎职位和部门的id在“addEmployee”变量中没有“id”属性

因此,要解决这个问题,只需从服务中的
addEmployee
中删除“id”

DefineEmployee(addEmployee: AddEmployee) {
  const body: AddEmployee = {

    Position: {
      id: addEmployee.Position // <<< here
    },

    Department: {
      id: addEmployee.Department  // <<< here
    }
  }
  return this.http.post('url', body);
}
defineeemployee(addEmployee:addEmployee){
const body:AddEmployee={
职位:{

id:addEmployee.Position//console log
addEmployee
DefineEmployee
中查看值的位置并正确使用它们。另一个建议是使用被动形式而不是模板驱动形式。请将
console.log(res)
的内容添加到question@TheFabio当我打开console.log(res)时它出现了错误类型错误:无法读取AddEmployeeService中未定义的属性“id”。我非常怀疑此错误是由
控制台生成的。log
调用…我将用我的thoughts@TheFabio是的,你在写,我很抱歉,出现的错误是:职位:身份证:未定义,部门:身份证:未定义好的,同样如此,但请检查我的问题我在AddEmployeeService中发布了代码,因为我已经在正文中添加了它,但没有结果如果您添加我建议的这些调试行,那么
console.log(addEmployee)有什么作用
product?它显示id的值已传递给addEmployee.Position,因此该值保留在Position,而id是未定义的。您可以分享
console.log(addEmployee)
生成的内容吗?[对象对象]