4

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

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks for your answer. But how can I specify to the function which tache I want to save?
I'm exactly working with a *ngFor="let myTache of Taches", and Taches= Array<Tache>; defined in my component. I can't see how can I use $event in this case?
I found your issue, extended answer, created plunker and added more details. We need to call Emitter not the function itself
Yes.. and there you can consume fired parameter with sauverTache)="sauverPlannings($event)" and in the code of sauverPlannings(myTacheId) you will consume that id... hope it helps a bit ;)
Thanks for the plunker. But I'm using my component in an other template, like this <my-component [projet]="projet" (sauverTache)="sauverPlannings()"> </my-component> . Should I replace sauverPlannings() by sauverPlannings($event)?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.