【问题标题】:Cannot set property 'matricule' of undefined angular无法设置未定义角度的属性“矩阵”
【发布时间】:2021-03-31 14:51:53
【问题描述】:

我正在尝试使用 Ionic 进行应用程序,但我收到此错误 Cannot set property 'matricule' of undefined 我不知道我在代码中缺少什么

当我想将数据添加到此表 employeeL 时,我收到此错误

我尝试做很多改变,但问题是一样的

当表格为空时我想访问 HTML 元素的问题 (问题在于同步,因为我正在从数据库中调用数据)

请帮忙,

这是代码 HTML:

<table class="table">
    <tr>
      <th>مجموع</th>
      <th>تم</th>
      <th>مثبت</th>
      <th>الاسم و اللقب</th>
      <th>عون الجرد</th>
    </tr>
    <tr *ngFor="let item of employeeL">
      <td (click)="getuserInfo(item?.matricule)">{{item?.mustBeDone}}</td>
      <td (click)="getuserInfo(item?.matricule)">{{item?.done}}</td>
      <td>
        <ion-checkbox slot="end" checked="{{item?.status}}"></ion-checkbox>
      </td>
      <td (click)="getuserInfo(item?.matricule)">{{item?.fullname}}</td>
      <td (click)="getuserInfo(item?.matricule)">{{item?.matricule}}</td>
    </tr>
  </table>

这里是 ts 文件:

 employeeL:Array<Awinjard> = [];
  inv_agentTab: Array<Inv_agent> = [];
  drafbiesTab: Array<Drafbies> = [];
  nameemployee: string = "";
  inv_agentNombre: number = 0;
  matriculeTable: Array<number> = [];
  nameTable: Array<string> = [];
  validatationTable: Array<number> = [];
  ngOnInit() {
    this.folder = this.activatedRoute.snapshot.paramMap.get('id');
    
    
    this.awinjard.getAwinjardMatricule().subscribe(res => {
      this.inv_agentTab = res as Array<Inv_agent>
      this.inv_agentTab.forEach(element => {
        this.matriculeTable[this.inv_agentNombre] = element.AGENT;
        this.validatationTable[this.inv_agentNombre] = element.VLD;
        this.inv_agentNombre++;
        this.awinjard.getAwinjardNameAr().subscribe(res2 => {
          this.drafbiesTab = res2 as Array<Drafbies>
          this.drafbiesTab=this.drafbiesTab.filter((employee)=>employee.DECAFFE==element.AGENT)
          this.nameemployee = this.drafbiesTab[0].AR_DELAFFE;
          console.log(this.drafbiesTab[0].AR_DELAFFE);
          this.nameTable.push(this.nameemployee);
        })
      });
      
      for (let i = 0; i <= 5; i++){
      //those elements that got me the error
        this.employeeL[i].matricule = this.matriculeTable[i];
        this.employeeL[i].fullname = this.nameTable[i];
       
      }

    })
  }

界面

export interface Awinjard {
    matricule: number,
    fullname:string ,
    status: boolean,
    done: number,
    mustBeDone: number,
}

【问题讨论】:

  • 可能是这一行:this.employeeL[i].matricule
  • 您将值放在employeeL 数组的什么位置?
  • 问题出在for 的ts 文件中,我将数据放入employeeL 中

标签: angular ionic-framework


【解决方案1】:

问题在于this.employeeL[i] 为空,因为您没有创建空的第 i 个元素。你在这里employeeL:Array&lt;Awinjard&gt; = []; 所做的只是创建一个没有元素的数组。 您应该使用push,这将在您的数组末尾创建一个包含您的新员工的新元素。

类似的东西

  for (let i = 0; i <= 5; i++){
     //Create employee(Awinjard) first
     let awin = new Awinjard()
     awin.matricule = this.matriculeTable[i]
     awin.fullname = this.nameTable[i];

     this.employeeL.push(awin)
   
  }

【讨论】:

  • 我应该将Awinjard interface 更改为类来制作对象吗??
  • 您可以将其保留为接口并创建像 {matricule:"New matricule", fullname:"New Name"...} 这样的对象。您也可以将其更改为对象并使用带有参数的构造函数来创建它。
  • let awin=new Awinjard() 这行给我带来了这个错误Awinjard' only refers to a type, but is being used as a value here
  • 请问您所说的创建员工是什么意思(Awinjard)
  • Awinjard 是类还是接口?
猜你喜欢
  • 2019-07-04
  • 2021-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多