Implementing a function when clicking on a set of items generated by ngFor loop

While working on my app, I managed to display individual flights in a horizontal list that users can scroll through. Here's what it looks like:

https://i.sstatic.net/oTwZS.png

Now, the next step for me is to make it so that when users click on a specific flight, its details will pop up on the screen. To achieve this, I need to figure out how to add a click event to each flight iteration, triggering the selectFlight method.

Here's a snippet of my code:

<h3>Flights </h3>
<div>
    <ul class= "grid grid-pad">
        <a *ngFor="let flight of flights" class="col-1-4">
            <li class ="module flight">
                <h4>{{flight.number}}</h4>
            </li>
        </a>
    </ul>
</div>

In order to achieve this functionality, I need to implement a click event like this: (click)="selectFlight() on every li element representing an individual flight.

I'm relatively new to Angular development with HTML & CSS, so I would appreciate any guidance or help from the community. Thank you in advance!

Edit: old flight-viewer.html

<!--
<!-- Omitted for brevity -->
-->

Do I need to include something like onFlightSelected ="flightSelected($event)" in my flight-viewer.html file?

Answer №1

details-viewer.html:

<div>
<ul class= "grid grid-pad">
    <a *ngFor="let detail of details" class="col-1-4">
        <li class ="module detail" (click)="selectDetail(detail)">
            <h4>{{detail.name}}</h4>
        </li>
    </a>
</ul>

detail.row.component.ts:

@Component({
selector: '[detail-row]',
templateUrl: 'app/detail-row.html',
    styleUrls: ['app/detail-row.css']
})

export class DetailRowComponent {
    @Input()
    detail: Detail;
    @Output()
    onDetailSelected = new EventEmitter<Detail>();
    @Output()
    onDetailUpdating = new EventEmitter<Detail>();
    name = 'DetailRowComponent';

selectDetail(detail) {
    console.log("Just selected ", this.detail.name, " for display");
    this.onDetailSelected.emit(this.detail);
}

You must include the parameter of detail in selectDetail(detail) and the function will retrieve specific information about that detail.

All the best. If you have any questions, feel free to leave a comment.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the process for dividing a form into two separate columns?

I am working on a form that sends input to a PHP script. I want to reorganize the form so that the second section (Person 2) appears in a column to the right of the first section (Person 1), instead of below it. <link rel="stylesheet" href="test.css" ...

How Angular's ui-router is Refreshing the Entire App Inside a ui-view

My goal is to develop a straightforward App using ui-router. Initially, when I load a page or refresh the current one, everything functions correctly. However, upon clicking on a different page, the entire app reloads in my ui-view and triggers a warning i ...

Ways to hide the loading spinner on a web browser

As the owner of a Comet application, I am facing an issue with long polling requests being sent via AJAX immediately after the page loads. These requests take a while to complete, causing browsers like Safari and Chrome to display their loading icon even t ...

Create a concise shorthand representation for margins when styling with jss using Material-UI's theme object

When styling the component, I found a way to apply the margin in a specific manner. style.js const styles = theme => ({ button: { margin: '12px 18px', } }) However, I wanted to utilize material-ui's theme.spacing.unit f ...

Having difficulties with CSS issues on jCarousel - struggling to figure it out

Before we proceed, take a look at this jsfiddle that I have prepared: I am aware of the importance of not relying on externally hosted code, so I will include it at the end of this question. My goal is to recreate the demonstration of the plugin availabl ...

Utilize celery as the foundation for your model structure

Unconventional Request A unique challenge has been presented to me: I am required to utilize celery for communication with the database layer. The application consists of Flask and MongoDB on the backend, while Angular is used on the frontend. The clien ...

Create a CSS menu that centers the links

Here is the CSS code I am using for my horizontal menu: nav { height: 40px; width: 100%; background: #F00; font-size: 11pt; font-family: Arial; font-weight: bold; position: relative; border-bottom: 2px solid # ...

Generating the templateUrl dynamically with ui-router

Looking to dynamically generate a template URL using the ID from my ng-click. However, running into an error: Uncaught Error: [$injector:modulerr] Failed to instantiate module mean due to: TypeError: filename.indexOf is not a function Below is my rou ...

Performing the .html() function on a div that magically deletes itself

I seem to be encountering an issue with the .html() function when used on a specific div element. Within my jQuery Dialog, there is a select option input housed within a div. Upon changing the value of the select option input, an ajax call is triggered to ...

How can I store the selected checkbox values in a variable or array when a button is clicked in Ionic 2?

Within my gridview, I have a list of email ids displayed. I am looking to gather the selected email ids in an array or variable upon button click but am struggling due to being new to this technology. Any assistance would be greatly appreciated. https://i ...

Identify the invalid field within an Angular form

After the form is rendered, I need to identify which fields are not valid after 5 seconds. Currently, I have a button that is set as ng-disabled="!step1Form.$valid". However, I would like to add a CSS class, possibly in red, to highlight the invalid fields ...

Using the map method to print from a massive array collection in sets of 20

Given a large array of numbers, I am attempting to divide it into sets of 20 numbers and print them. However, when I try to iterate through the array with an if condition, I get undefined values. I thought of creating a result array and pushing the subset ...

What could be the reason my spin animation is only triggered once upon button press?

Every time the submit button is clicked, I want all the images to spin. However, this rotation effect only works the first time the button is pressed, until the page is refreshed. I have used jQuery to add and remove a class from the images when the button ...

What's causing the click event to only work on every second li element?

Let's tackle a simple to-do list scenario. Imagine inputting tasks and clicking submit, which then adds an item to the list. Clicking on an item should mark it with a line through the text. However, a peculiar issue arises: when multiple items are add ...

What is the recommended approach for displaying data enclosed in double quotes in PHP?

Looking to store HTML text in a variable without using ?> .... <?. The challenge lies with the quotes — wanting to have double quotes instead of single quotes. Using single quotes for variables with interpolation doesn't work: $var = ' ...

Encountering issues with the display of JSON data in MVC

Question: How can I pass JSON data from my controller to the view and render it using JavaScript? [HttpGet] [AllowAnonymous] public ActionResult Index() { ServiceReference1.ipostep_vP001sap0003in_WCSX_comsapb1ivplatformruntime_INB_WS_C ...

Chrome scrolling causing Webkit mask to shift position

I have successfully created a rounded Google map after much effort. Here is the link to the JSFiddle: http://jsfiddle.net/DZTvR/13/ However, I encountered an issue in Chrome where the mask remains static while scrolling, unlike other browsers like FF, Op ...

Using CSS to position a pair of divs containing text and images side by side

Is there a better approach to creating two divs and two pictures without using negative margins, similar to the example shown here? ...

A fun twist on uploading a file with Python Mechanize

Hey everyone, I'm new to Mechanize and still learning the ropes! I want to automate uploading a .torrent file to a private tracker by completing a form with one or two file upload fields. The kind where you click and browse for the file. The issue i ...

A password must contain a minimum of 2 uppercase letters, 2 lowercase letters, 2 symbols, and 2 numbers in any order according to the regular expression

I'm having trouble implementing this RegEx pattern in JavaScript for password validation: (?=(.*\\d){2,})(?=(.*[A-Z]){2,})(?=(.*[a-z]){2,})(?=(.*[!@#$%^&*?]){2,})(?!.*[\\s])^.* ...