Naella
Naella

Reputation: 445

TypeError: self.parent.parent.parent.context is not a function Angular 2

I've created this function to save my 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}}&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>

and I got this error

TypeError: self.parent.parent.parent.context.sauverTache is not a function

Upvotes: 4

Views: 11556

Answers (1)

Radim K&#246;hler
Radim K&#246;hler

Reputation: 123891

The way how to get to argument emitted by the event is just via keyword $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

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:

  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

Upvotes: 5

Related Questions