Nae*_*lla 4 typescript angular
我创建了这个函数来保存我的 taches
sauverTache(tache:Tache){
this.editionEnCours = true;
tache.estReelle = true;
this.sauverTache.emit(tache.id);
}
Run Code Online (Sandbox Code Playgroud)
我像这样在模板中调用我的函数
<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>
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误
TypeError: self.parent.parent.parent.context.sauverTache is not a function
Run Code Online (Sandbox Code Playgroud)
如何获取事件发出的参数只是通过关键字$event:
//(click)="edit=!edit; sauverTache(myTache);"
(click)="edit=!edit; sauverTache($event);"
Run Code Online (Sandbox Code Playgroud)
如果您需要来自某个迭代数组的参数,您也可以传递它
<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>
Run Code Online (Sandbox Code Playgroud)
如果我们需要从组件类中进行一些设置,我们也可以
class MyComponent {
public myTache: number;
ngOnInit() {
this.myTache = 1;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我们可以要求将其作为原始片段传递
(click)="edit=!edit; sauverTache(myTache);
Run Code Online (Sandbox Code Playgroud)
简单地说,我们要么必须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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11233 次 |
| 最近记录: |