Interactive Modal Featuring Image Slider - Capture Names and Sequential Numbers

I successfully created a modal with an image slider using the bootstrap ngb-carousel.

Is there a way to dynamically display the current image's name in the modal header and the numbering of images in the modal footer?

For example, I want the modal header to show the name of the current image, and the footer to display the image number out of total images. So, if I have 3 images and I am on the first one, the footer should display '1/3'. This should update as I change images on the slider.

If anyone has suggestions or solutions for implementing this functionality, it would be greatly appreciated! I haven't been able to figure out how to achieve this so far.

DEMO

Code

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
Show images Modal
</button>

<div class="modal" id="myModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">Name Image !</h4>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <div class="modal-body">
                <ngb-carousel id="carousel" #carouse *ngIf="data" (slide)="change($event)">
                    <ng-template *ngFor="let imgIdx of data; let i = index" [id]="i" ngbSlide>
                        <div class="picsum-img-wrapper">
                            <img [src]="imgIdx.image" [alt]="">
                          </div>
                            <div class="carousel-caption">
                                <h3>{{imgIdx.head}}</h3>
                                <p>{{imgIdx.data}}</p>
                            </div>
                    </ng-template>
                </ngb-carousel>
            </div>
            <div class="modal-footer">
                Number images (1/3)
            </div>
        </div>
    </div>
</div>

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

Answer №1

To begin, simply declare three variables:

  // Make sure to manually assign initial values first

  imgName:string=this.data[0].name
  imgNumber:number=1
  imgLength:number=this.data.length

In the change function, update these variables like so:

  change(data){
    this.imgNumber=data.current+1;
    this.imgName=this.data[data.current].name
  }

Your .html file can utilize these variables as follows:

<div class="modal" id="myModal">
            ...
        <h4 class="modal-title">{{imgName}}!</h4>
            ....
        <div class="modal-footer">
            Number images ({{imgNumber}}/{{imgLength}})
        </div>
        ...
</div>

For more information on displaying data and interpolation, refer to the official documentation.

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

"Exploring the Power of WebSocket Proxies in Angular 5.0

I've been attempting to proxy websockets using angular CLI, but unfortunately, it isn't working as expected. Below is my proxy configuration in proxy.conf.json { "/stream/*": { "target": "ws://demos.kaazing.com/echo", ...

Property finally is missing in the Response type declaration, making it unassignable to type Promise<any>

After removing the async function, I encountered an error stating that the Promise property finally is missing when changing from an async function to a regular function. Any thoughts on why this would happen? handler.ts export class AccountBalanceHandle ...

When using Angular mat select, the selected text should be displayed instead of the default dropdown text

I am currently utilizing Angular 8 along with the latest version of Angular Material. I have a mat form field where the selection functionality is working fine, but the styles are not displaying correctly and overriding the text placement for the selected ...

What could be causing the issue with the conditional validation in my zod and react-hook-form integration?

Hello there! I recently refactored my code and encountered a problem with a conditional field. When the user selects "Yes," an additional field should appear below for them to fill out. However, the issue I'm facing is that when "Yes" is selected, the ...

Just starting out with Angular and encountering an issue where my variables are not recognized even though they are clearly defined

Despite my page functioning correctly, I am encountering an error in my VScode. It seems like I might be making a mistake even though everything appears to be working fine. The issue arises when I attempt to add objects to a new array outside of my ngOnIn ...

Creating a stream of observables in RxJs and subscribing to only the latest one after a delay: A comprehensive guide

I am trying to create a stream of Observables with delay and only subscribe to the last one after a specified time. I have three HostListeners in which I want to use to achieve this. I would like to avoid using Observable form event and instead rely on H ...

Incorporating properties of the parent class through Mixin techniques

I'm encountering an issue while trying to access a property from an inherited class within a mixin class BaseItem{ public id:string; constructor(id:string) { this.id =id; } } abstract class ConfigMixin<K extends BaseItem& ...

Is there a way to differentiate between a plain object and a class instance in Typescript?

Specifically, I am looking to differentiate between primitive types and plain objects versus class instances. let x = {y:5} // this is acceptable class X { y = 5; } let x = new X(); // this is not permissible ...

Converting a JSON array stored in a local file to a TypeScript array within an Angular 5 project

I'm currently working on developing a web app using Angular 5. My JSON file has the following structure: [ { "id": 0, "title": "Some title" }, { "id": 1, "title": "Some title" }, ... ] The JSON file is store ...

Difficulty in monitoring the present machine status through XState in a React application

I'm encountering an issue where I am trying to access the Machine state from within a function in a React component using state.value. However, the current state never changes and it always displays the initial state. Strangely, if I include an onClic ...

Creating a visually appealing stacked bar chart using Chart.js in Angular 9 with multiple bars

Upon integrating Chart.js into my Angular 9 application, I encountered an issue where the expected chart values were not being displayed. In order to address this problem, I need to structure the barChartData according to the format showcased in this stac ...

The "ngx-places" directive does not have an "exportAs" setting

I recently added the ngx-google-places-autocomplete module to my project. However, upon configuring it, I encountered the following error: core.js:14597 ERROR Error: Uncaught (in promise): Error: Template parse errors: There is no directive with "expo ...

I am encountering a strange error in the console when trying to implement a custom navigation system tailored to each user using the core UI Angular

An error occurred at line 33 in the AppSidebarNavItemsComponent.html file. The error message states: "ERROR TypeError: Cannot read property 'length' of undefined". Here is the sequence of events leading up to this error: - The createUrlTree funct ...

Using Primeng to implement pagination and lazy loading in a dataView

Looking for a way to search through product data with filters and display it using primeng dataview? Consider implementing pagination in your API that pulls products from the database page by page. Set the products array as the value in dataview so that wh ...

Tips for implementing [disabled] directive with dual options in Ionic Angular

Check out my page.html code: <ion-content *ngIf="charged"> Order ID: {{order.id_acg}} Item Weight(g): Height(cm): Width(cm): Length(cm): ...

Querying: What is preventing me from accessing a button within a modal?

I am currently utilizing jQuery AJAX to fetch the complete contents of a Bootstrap modal. Here is the accompanying JavaScript code: $.get('/user/modal', { id : datum.id }).done(function (data) { $(data).modal('toggle'); $(& ...

Previewing multiple images in multiple divs before uploading using jQuery

Currently, I am working on a Bootstrap Modal that allows users to upload multiple images. The functionality is such that when the user clicks the 'Add-Product-Image' button, it generates a new image upload input form. Once the user selects an ima ...

Splitting a td tag into multiple columns dynamically with Angular

I am attempting to dynamically split the table element into separate columns. My desired layout should resemble this: https://i.sstatic.net/C81tg.png The values for name and surname are fixed at 1, but the values for subjects and grades can vary (there ma ...

What steps do I need to take to enable the about page functionality in the angular seed advance project?

After carefully following the instructions provided on this page https://github.com/NathanWalker/angular-seed-advanced#how-to-start, I successfully installed and ran everything. When running npm start, the index page loads with 'Loading...' I ha ...

Angular 6 Error: Failed to parse template. Unable to establish a connection with 'routerLink' as it is not recognized as a valid property of 'a'

During app testing with npm test An error is encountered : Failed: Template parse errors: Can't bind to 'routerLink' since it isn't a known property of 'a'. (" <nav> <ul> <li><a class=" ...