我创建了这个函数来保存我的taches
sauverTache(tache:Tache){ this.editionEnCours = true; tache.estReelle = true; this.sauverTache.emit(tache.id); }
<div class="sbold tr" *ngFor="let tache of etapes.P0.taches,let i=index" [class.hidden]="!afficheTaches" > <td align="right">{{tache.typeTache}} </td> <td> <div> <p-calendar [(ngModel)]="etapes.P0.taches[i].dateTache" showAnim="slideDown" [class.hidden]="!editP0[i]" dateFormat="dd/mm/yy" placeholder="jj/mm/aaaa"></p-calendar> <div class="btn btn-circle btn-default font-yellow-saffron" *ngIf="!editP0[i]" (click)="editP0[i]=!editP0[i]"> <i class="fa fa-pencil "> </i> </div> <div class="btn btn-circle btn-default font-green-jungle" *ngIf="editP0[i]" (click)="editP0[i]=!editP0[i]; sauverTache(etapes.P0.taches[i]);"> <i class="fa fa-check "> </i> </div> <div class="btn btn-circle btn-default font-red" *ngIf="editP0[i]" (click)="editP0[i]=!editP0[i]; reset();"> <i class="fa fa-remove "> </i> </div> </div> </td> </div>
我收到了这个错误
TypeError: self.parent.parent.parent.context.sauverTache is not a function
解决方法
如何获取事件发出的参数只是通过关键字$event:
//(click)="edit=!edit; sauverTache(myTache);" (click)="edit=!edit; sauverTache($event);"
如果您需要来自某个迭代数组的参数,您也可以传递它
<div *ngFor="let myTache of Taches"> ... <div class="btn btn-circle btn-default font-green-jungle" *ngIf="edit" (click)="edit=!edit; sauverTache(myTache);"> <i class="fa fa-check "> </i> </div> ... </div>
如果我们需要从组件类中进行一些设置,我们也可以
class MyComponent { public myTache: number; ngOnInit() { this.myTache = 1; } }
现在我们可以要求将其作为原始片段传递
(click)="edit=!edit; sauverTache(myTache);
简单地说,我们要么让myTache成为局部变量(通常是ngFor的一部分),要么是我们组件上的属性.如果我们需要使用事件参数 – 我们应该要求$event
延伸
最大的问题是在sauverTache里面,我们想要发出一些数据.这必须在EventEmitter的帮助下完成:
sauverTacheObservable = new EventEmitter(); sauverTache(tache: Tache){ this.editionEnCours = true; tache.estReelle = true; // this is self calling.. and causing problem... // this method does not have method emit //this.sauverTache.emit(tache.id); // but now,we call proper emitter this.sauverTacheObservable.emit(tache.id); console.log(tache.id); }