3

I'm trying to create a list of components and to attach a click event listener that registers the index of the component clicked.

I can achieve this by attaching the event listener at my leaf node, as it knows its index. But then I have to bubble the event up one more time than I might need to. What I'd prefer to do is have the following at the parent of the leaves:

  template: `
    <trackpoint
        *ngFor='#tp of lapData; #i=index'
        [tp]='tp'
        [index]='i'
        [ngClass]="{selected:selectedTps[i]}"
        (click)='handleClick($event)'>
    </trackpoint>
  `,

and in my controller

handleClick(e:MouseEvent) {
   console.log(e);
   this.lapEventHandler.next({
   // I want to be able to access i from the template
  });
}

I've been examining the the MouseEvent object but although I can find the Trackpoint among the event parents, I cannot find the index property. Am I trying to do the impossible.

2
  • Maybe I'm missing something but your code seems to add the event handler on each generated code but you explain that this is not what you actually want. I don't understand your question. Commented Jan 14, 2016 at 9:23
  • I started with the click handler in the trackpoint component, but I wanted to attach to trackpoint's parent - the code you see. Vlado provided a solution Commented Jan 14, 2016 at 9:26

1 Answer 1

7

Try with

(click)='handleClick($event, i)

and

handleClick(e:MouseEvent, i: any)
Sign up to request clarification or add additional context in comments.

Comments

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.