加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

typescript – TypeError:self.parent.parent.parent.context不

发布时间:2020-12-17 17:26:26 所属栏目:安全 来源:网络整理
导读:我创建了这个函数来保存我的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=
我创建了这个函数来保存我的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}}&nbsp;</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);
  }

有a working plunker

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读