无法删除模式中的特定用户

cannot delete a particular user in modal

我正在尝试删除特定用户。单击删除按钮时,我能够做到这一点。按钮现在我添加了一个显示确认的模式,如果我确定我要删除或不。但是,当我单击模式上的删除按钮时,我收到 属性 undefined.How 的错误来解决它?

component.html

<tbody>
        <tr *ngFor="let person of persons">
          <td>{{ person.user.id }}</td>
          <td>{{ person.user.first_name}}</td>
          <td>{{ person.user.last_name }}</td>
          <td>{{ person.user.email }}</td>
          <td>{{ person.role }}</td>
          <td>
            <label class="switch" *ngIf="Authentication.roleMatch(['change status'])">
                <!-- <p *ngIf="Authentication.roleMatch(['change status'])">
                    <input type="checkbox" (click)="toggle(person)" [(ngModel)]="person.user.is_active" [disabled]="person.user.is_superuser=== true">
                  </p> -->

                  <input  type="checkbox" (click)="toggle(person)" [(ngModel)]="person.user.is_active" [disabled]="person.user.is_superuser=== true">

                <span class="slider round"></span>
            </label>
        </td>
        <td>
        <p *ngIf="Authentication.roleMatch(['edit user'])">
            <a  class="edit" (click)="onSelect(person)" data-toggle="modal" data-target="#editModal"><img src="../../../assets/images/edit.png" alt=""></a> 
          </p>
          </td>

          <td>

**//delete modal**

          <p *ngIf="Authentication.roleMatch(['delete user'])">
              <a  class="delete"  data-toggle="modal" data-target="#deleteModal" ><img src="../../../assets/images/delete (1).png" alt=""></a>
          </p>

        </td>
        </tr>
      </tbody>


<!-- Delete User -->
<!-- The Modal -->
<div class="modal" id="deleteModal">
<div class="modal-dialog">
<div class="modal-content">


  <!-- Modal body -->
  <div class="modal-body">
    Are you sure you want to delete this user?
    <br>

    <div>

     //error shows here
      <a (click)="delete_user(person.user.id)"><i class="fa fa-check right"  aria-hidden="true"></i></a>
      <a ><i class="fa fa-times wrong" data-dismiss="modal" aria-hidden="true"></i></a>
    </div>

  </div>


</div>

component.ts

//Delete user

delete_user(id){

this.Authentication.delete_user(id).subscribe(
data =>{
  console.log('deleted user');
 this.getUsersFromServices();

},err => {
  console.log('error occured while deleting user');
}
)
}

这是因为它没有获取 person 的值,因为模态在 *ngFor 之外,所以你可以像这样为要删除的项目创建一个变量 component.ts

itemToBeDeleted:any;//declare the variable

在html

<tbody>
        <tr *ngFor="let person of persons">
          <td>{{ person.user.id }}</td>
          <td>{{ person.user.first_name}}</td>
          <td>{{ person.user.last_name }}</td>
          <td>{{ person.user.email }}</td>
          <td>{{ person.role }}</td>
          <td>
            <label class="switch" *ngIf="Authentication.roleMatch(['change status'])">
                <!-- <p *ngIf="Authentication.roleMatch(['change status'])">
                    <input type="checkbox" (click)="toggle(person)" [(ngModel)]="person.user.is_active" [disabled]="person.user.is_superuser=== true">
                  </p> -->

                  <input  type="checkbox" (click)="toggle(person)" [(ngModel)]="person.user.is_active" [disabled]="person.user.is_superuser=== true">

                <span class="slider round"></span>
            </label>
        </td>
        <td>
        <p *ngIf="Authentication.roleMatch(['edit user'])">
            <a  class="edit" (click)="onSelect(person)" data-toggle="modal" data-target="#editModal"><img src="../../../assets/images/edit.png" alt=""></a> 
          </p>
          </td>

          <td>

**//delete modal**

          <p *ngIf="Authentication.roleMatch(['delete user'])">
              <a  class="delete"  data-toggle="modal" data-target="#deleteModal" (click)="itemToBeDeleted=person" ><img src="../../../assets/images/delete (1).png" alt=""></a>
          </p>

        </td>
        </tr>
      </tbody>


<!-- Delete User -->
<!-- The Modal -->
<div class="modal" id="deleteModal">
<div class="modal-dialog">
<div class="modal-content">


  <!-- Modal body -->
  <div class="modal-body">
    Are you sure you want to delete this user?
    <br>

    <div>

     //error shows here
      <a (click)="delete_user(itemToBeDeleted?.user?.id)"><i class="fa fa-check right"  aria-hidden="true"></i></a>
      <a ><i class="fa fa-times wrong" data-dismiss="modal" aria-hidden="true"></i></a>
    </div>

  </div>


</div>