Add an item to the Bootstrap modal in a creative way

Trying to implement a yes/no modal dialog service using bootstrap modal has been a challenge for me. I am struggling with how to inject text data (title and message) into the modal controller.

Here is the code snippet for the Yes/No modal controller:

module Dialogs
{
export class YesNoDialog
{
    static $inject = ['$scope', 'YesNoDialogConfig'];

    Title: string;
    Message: string;

    constructor($scope, YesNoDialogConfig)
    {
        $scope.vm = this;            

        this.Title = YesNoDialogConfig.Title;
        this.Message = YesNoDialogConfig.Message;
    }        
}
}

And here is the code for the service handling the yes/no dialog:

module DialogServices
{
export class YesNoDialogService
{
    $inject = ['$modal'];

    ModalService: ng.ui.bootstrap.IModalService;

    constructor($modal)
    {
        this.ModalService = $modal;
    }

    ShowModal(Title: string, Message: string) : ng.IPromise<any>
    {
        return this.ModalService.open(
            {
                templateUrl: 'App/Views/Dialogs/YesNoDialog.html',
                controller: Dialogs.YesNoDialog,
                resolve: {
                    YesNoDialogConfig:
                    {
                        Title: Title,
                        Message: Message
                    }
                }

            }).result;
    }
}
}

However, I encountered an issue where YesNoDialogConfig is undefined in the modal's constructor. I attempted a different approach by creating a YesNoDialogConfig class, populating it with data, and then calling it like this:

    ShowModal(Title: string, Message: string) : ng.IPromise<any>
    {
        this.ModalConfig = new Models.YesNoDialogConfig(Title, Message);

        return this.ModalService.open(
            {
                templateUrl: 'App/Views/Dialogs/YesNoDialog.html',
                controller: Dialogs.YesNoDialog,
                resolve: {
                    YesNoDialogConfig: function ()
                    {
                        { return this.ModalConfig }
                    }

            }).result;
    }

Unfortunately, this approach did not work either. Any suggestions or insights on how to solve this issue would be greatly appreciated. Thank you!

Answer №1

It turns out that my lack of knowledge was the cause of my struggle. Initially, I tried to inject a concrete yes-no config class into the modal controller:

export class YesNoDialog
{
    static $inject = ['$scope', 'YesNoDialogConfig'];

    Title: string;
    Message: string;        

    constructor($scope, YesNoDialogConfig: Dialogs.YesNoDialogConfig)
    {
        $scope.vm = this;            

        this.Title = YesNoDialogConfig.Title;
        this.Message = YesNoDialogConfig.Message;
    }
}

The actual yes-no config class looks like this:

export class YesNoDialogConfig
{
    Title: string;
    Message: string;

    constructor(Title: string, Message: string)
    {
        this.Title = Title;
        this.Message = Message;
    }
}

However, this approach did not work as expected. So, I decided to create a yes-no config interface and inject that instead. In the method where I call the modal dialog, I resolved a new instance of the concrete class:

The updated controller:

export class YesNoDialog
{
    static $inject = ['$scope', 'YesNoDialogConfig'];

    Title: string;
    Message: string;        

    constructor($scope, YesNoDialogConfig: Dialogs.IYesNoDialogConfig)
    {
        $scope.vm = this;           

        this.Title = YesNoDialogConfig.Title;
        this.Message = YesNoDialogConfig.Message;
    }
}

The new interface:

export interface IYesNoDialogConfig
{
    Title: string;
    Message: string;
}

export class YesNoDialogConfig implements IYesNoDialogConfig
{
    Title: string;
    Message: string;

    constructor(Title: string, Message: string)
    {
        this.Title = Title;
        this.Message = Message;
    }
}

When calling from the service:

return this.ModalService.open(
            {
                templateUrl: 'App/Views/Dialogs/YesNoDialog.html',
                controller: Dialogs.YesNoDialog,
                resolve: {
                    YesNoDialogConfig: function ()
                    {
                        return new Dialogs.YesNoDialogConfig(Title, Message);
                    }
                }

            }).result;

And lo and behold, it works like a charm :)

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

The compiler is unable to locate the node_module (Error: "Module name not found")

Error: src/app/app.component.ts:4:12 - error TS2591: Cannot find name 'module'. Do you need to install type definitions for node? Try npm i @types/node and then add node to the types field in your tsconfig. 4 moduleId: module.id, When att ...

The functionality of the Bootstrap4 accordion is not functioning according to my expectations

My goal is to create a unique e-commerce checkout page design. Each panel would be opened sequentially, with the next panel unfreezing when the action button of the current panel is clicked. However, I seem to be making a mistake as it is not working as in ...

Hiding a div using swipe gestures in Angular

What am I trying to achieve? I aim to hide a div when swiped right. This specific action will close the pop-up that appears after clicking a button. What tools are at my disposal? I am utilizing Ionic framework for this task. Within my app.js, I have i ...

Angular, manipulating components through class references instead of creating or destroying them

I am exploring ways to move an angular component, and I understand that it can be achieved through construction and destruction. For example, you can refer to this link: https://stackblitz.com/edit/angular-t3rxb3?file=src%2Fapp%2Fapp.component.html Howeve ...

Establishing the controller to set the default order

Would appreciate some assistance with what may appear to be a beginner's question, please? This is the HTML code I'm working with: <!doctype html> <html> <head> <title>Starting Angular</title> </head> < ...

What is the best way to set up my page to detect the "enter" key input when the form is not created with the <form> tag?

Within the HTML code, data is received and stored in variables within my TypeScript file. At the end of the HTML, there is a button that was created separately from any user input containers. This button triggers a function that processes the information i ...

Coverage of code in Angular2 using Qunit

Is there a reliable code coverage measurement tool or framework that can easily be integrated to assess the code coverage of Angular2-TypeScript code with QUnit tests? I have come across some frameworks like remap-istanbul, blanket.js etc., but these fram ...

Implement a nested filter in Angular 8 to enhance data manipulation

Is it possible to apply a filter inside another filter in this scenario? I have a table of orders with nested tables of services, and I want to only display the orders where the type of service is 'type1'. I tried using the following line of code ...

The issue with the antd Input component's onChange event not updating state correctly when using a list fetched from an axios get request in a React

Recently delving into React, I've dedicated the past week to honing my skills. My current project involves creating a straightforward application featuring a 'product create' form alongside a product list equipped with a search bar (utilizin ...

The saved editable input number is automatically pushed even without needing to click on save or cancel

I am working with a datatable, chart, and a label that shows the latest added value. The table and chart display time-series data for the last 30 minutes, including the timestamp and a random numerical value between 0 and 999. Every 10 seconds, a new data ...

bootstrap style list-group without overlapping

In the image below, you can see that I have a list of American states within a list-group class. My issue is that I am attempting to eliminate the whitespace and essentially move Arkansas below Alaska and so forth. Here is my current HTML and CSS: CodePen ...

AngularJS ng-repeat with integrated Highcharts

Is it possible to have multiple charts (using highcharts) on the same page within a loop? It seems to be causing issues. Typical scenario (works as intended): <div ng-attr-id="container1" style="min-width: 400px; height: 400px; max-width: 200p ...

"The CURL request is functioning properly, whereas the Ajax request is not yielding the

I currently have a Scrapyd server up and running and I am attempting to schedule a job. When I use the following CURL command, everything works perfectly: curl http://XXXXX:6800/schedule.json -d project=stackoverflow -d spider=careers.stackoverflow.com - ...

Angular HTML fails to update correctly after changes in input data occur

Within my angular application, there is an asset creator component designed for creating, displaying, and editing THREE.js 3D models. The goal was to implement a tree-view list to showcase the nested groups of meshes that constitute the selected model, alo ...

Angular in conjunction with socket.io does not immediately show messages on screen

I am currently working on developing an instant messaging app (chat) using socket.io and Angular. I have two main files: index.html and index.js as shown below. The chat functionality is working well, but I am facing an issue where the messages do not appe ...

Displaying an array's values in an ng-repeat using a JSON object output

I have received JSON output from an API and I want to display it using ng-repeat within a div. [ { "companyName": "abc", "namesList": [ { "name": "Jaakr1", "email": "& ...

What is the best way to retrieve the second to last element in a list

When using Protractor, you have convenient methods like .first() and .last() on the ElementArrayFinder: var elements = element.all(by.css(".myclass")); elements.last(); elements.first(); But what about retrieving the element that comes right before the ...

Troubleshooting logic errors and repetitive functions in AngularJS

My code using AngularJS is experiencing a logic error. I have created a function that iterates through a JSON array and retrieves the weather conditions as strings, such as 'clear', 'cloudy', etc. The function then compares these string ...

Adding typing to Firebase Functions handlers V2: A step-by-step guide

Here's a function I am currently working with: export async function onDeleteVideo(event: FirestoreEvent<QueryDocumentSnapshot, { uid: string }>): Promise<any> { if (!event) { return } const { disposables } = event.data.data() ...

Repair the bottom section on Bootstrap

I've successfully made the image stick to the footer, but I'm struggling to get the footer to stick to the bottom when in responsive mode. I'm new to bootstrap, so any help would be greatly appreciated. HTML: The footer is located after the ...