typescript 类型错误:self.parent.parent.parent.context 不是函数 Angular 2
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37614341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: self.parent.parent.parent.context is not a function Angular 2
提问by Naella
I've created this function to save my taches
我创建了这个函数来保存我的 taches
sauverTache(tache:Tache){
this.editionEnCours = true;
tache.estReelle = true;
this.sauverTache.emit(tache.id);
}
I call my function in the template like this
我像这样在模板中调用我的函数
<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>
and I got this error
我收到了这个错误
TypeError: self.parent.parent.parent.context.sauverTache is not a function
回答by Radim K?hler
The way how to get to argument emitted by the eventis just via keyword $event
:
如何获取事件发出的参数的方法只是通过关键字$event
:
//(click)="edit=!edit; sauverTache(myTache);"
(click)="edit=!edit; sauverTache($event);"
In case you need the parameter coming from some iterated array, you can pass it as well
如果您需要来自某个迭代数组的参数,您也可以传递它
<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>
And in case that we need some setting from our component class, we can also
如果我们需要组件类的一些设置,我们也可以
class MyComponent {
public myTache: number;
ngOnInit() {
this.myTache = 1;
}
}
and now we can ask for that to be passed as in original snippet
现在我们可以要求在原始片段中传递它
(click)="edit=!edit; sauverTache(myTache);
Simply, we either need to have myTache
to be a local variable (usually part of ngFor) or a property on our component. If we need to consume events arguments - we should ask for $event
简单地说,我们要么必须myTache
是一个局部变量(通常是 ngFor 的一部分),要么是我们组件上的一个属性。如果我们需要使用事件参数 - 我们应该要求 $event
EXTEND
延长
The biggest issue is inside of the sauverTache, where we want to emit some data. That must be done with a help of EventEmitter:
最大的问题是在 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);
}
There is a working plunker