0

This is a little bit harder to explain, but I will do my best.

I have a component called feed, and the component it expects an array(feeds) of objects(feed). I have a template which i display all the feeds and the way i create them is using ngFor.

E.g it creates div containers for each one of them individually.

Basically I'm trying identifying the specific feed i clicked on, rather than all of them with that feed-container class...Because of course, it is shared based on how many feed items we have in the array.

The problem I'm facing is that I want to be able to click on one(1) feed and console log something. How i achieve this is by creating this:

@HostListener('click', ['$event'])
  feedClickEvent(event: any) {
    console.log(event);
    this.feedListModel.map((feedItem, index) => {
      const x = document.getElementsByClassName(index.toString());

      if (x[0].id === feedItem.id) {
        //console.log("I`m triggered ", feedItem.id);
      }
    });
  }

However, I would like to get the current div id which is clicked on but i don't know exactly how, I've tried many different scenarios but they didn't work.

The feedListModel is the input to the component with the object array and based on that i create the individual divs(feeds).

Here is the HTML template:

<div class="container">
    <div class="row feed-container" id="{{feedItem.id}}" *ngFor="let feedItem of feedListModel; let i = index">
        <div class="feed-item {{i}}" id="{{feedItem.id}}">
            <div class="col-sm-4 column-inlineBlock feed-avatar">
                <span>
                    <k-icon [icon]=" { type: 'image', src: 'someUrlPhoto', extraClass: 'feed-icon'}"></k-icon>
                </span>
            </div>
            <div class="col-sm-7 column-inlineBlock main-feed-content">
                <div class="title"><strong>{{feedItem.secondColumn.title}}</strong></div>
                <div class="description">{{feedItem.secondColumn.description}}</div>
                <div class="footer" *ngIf="!feedItem.secondColumn.footer.isTimeAgo">{{feedItem.secondColumn.footer.value}}</div>
                <div class="footer time-ago" *ngIf="feedItem.secondColumn.footer.isTimeAgo">
                    <k-icon [icon]="{type: 'fas', name: feedItem.secondColumn.footer.icon.icon, extraClass: 'icon-clock'}"></k-icon>
                    {{feedItem.secondColumn.value | timeAgo}}
                </div>
            </div>
            <div class="col-sm-1 column-inlineBlock third-col" *ngIf="!isTwoColumn">
                <div class="third-col-wrapper">
                    <span class="icon-indicator {{feedItem.thirdColumn.status}}">
                        <k-icon [icon]="{type: 'fas', name: feedItem.thirdColumn.kIcon.icon, extraClass: 'icon-number'}"></k-icon>
                </span>
                <span class="number-indicator">
                    <strong id="value-indicator">{{feedItem.thirdColumn.value}}</strong>
                </span>
                </div>
            </div>
        </div>
    </div>
</div>

For instance, in the hostlistener i console log the event.currentTarget and i get 2 divs, even though i clicked on the first div(feed). In other words, there are 2 feeds. And i need the clicked one only with the ID of it so that i can do something with it.

enter image description here

3
  • Did you try event.target.id? Commented May 7, 2020 at 14:41
  • yes, but it console logs an empty row, or if i click in the container-feed div, it gets the id(which this is what i want) but if i dont click in the container, it doesn`t display anything...And all i want, is to display that id regardless on which div you click, as long as it is in the main container of the specific div clicked. Does it makes sense? Commented May 7, 2020 at 14:44
  • Or another scenario would be to somehow loop through the event.path and stop on the first div with an id. But i'm not sure how i would do that in order to get that id. Commented May 7, 2020 at 14:51

1 Answer 1

1

If I have understood, you just need to bind a click event on element in template and add a method in the component to do what you want:

in the template:

<div class="container">
    <div class="row feed-container" (click)="select(feedItem)" id="{{feedItem.id}}" *ngFor="let feedItem of feedListModel; let i = index">
         ...
    </div>
</div>

in the component:

public select(feedItem: any) {
    // your logic here
}
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.