首页 > 解决方案 > 由于 ngIf,参数未通过

问题描述

这是 .html 代码

<div class="box col-lg-3 sidebar col-3">
  <ul style="overflow-x: hidden; overflow-y:scroll">
      <ng-container *ngFor="let user of products" [class.selected]="user === selectedPatient" >
         <li  *ngIf="filter(user)" (click)="onSelect(user);" >
            <p>{{ user.name }} {{ filtername }}</p>
        </li>
      </ng-container>
  </ul>
</div>
<div class="box col-lg-9 mainpage col-9 " style="padding:0px;">
  <app-doctor-patientview  [patient]="selectedPatient" ></app-doctor-patientview>
  <!--  -->
</div>

这是上面 html 代码的 component.ts 代码

  products: any[];
  name: any;
  selectedPatient: Patient;
  filtername: string="";
  show:boolean = true;

onSelect(hero: Patient): void {
  this.selectedPatient = hero;
  console.log(this.selectedPatient);
}

filter(patient : Patient) : boolean{

  if (patient.name.toUpperCase().indexOf(this.filtername.toUpperCase())>-1){
    return true;
  }
  return false;
}

这是 app-doctor-patientview.html

<div *ngIf="patient"></div>

app-doctor-patientview component.ts 的代码片段

    export class DoctorPatientviewComponent implements OnInit {
  @Input() patient: Patient;
  isOn=1;

  constructor() { }

  ngOnInit() {
  }

}

似乎在 .html 代码中包含 *ngIf 时,selectedPatient 参数没有传递给 app-doctor-patientview。

如果我不包含 *ngIf 参数通过并在 app-doctor-patientview 中正确显示。我该如何解决这个错误?

标签: angularangular6angular-ng-if

解决方案


推荐阅读