Is it possible to trigger a modal from a service in AngularStrap?

My web app utilizes AngularJS, Bootstrap 3, and AngularStrap. Initially, I used Bootstrap UI for directives but had to switch to AngularStrap for added functionality like custom popover templates. This migration impacted the implementation of the modal directive, which is what my query revolves around.

Previously, with Bootstrap UI, a security service triggered a login modal when users tried accessing restricted content using a controller from another module. Here's a simplified version of the code:

// Login form controller:

angular.module('LoginForm', []).controller('LoginFormController', ['$scope', 'security', function($scope, security) {

    /* Controller logic for login, cancel, clear, etc. */

}]);

// Security service:

angular.module('Security', ['ui.bootstrap','LoginForm']).factory('security', ['$modal', function($modal) {

    var loginDialog = null;
    function openLoginDialog() {
        loginDialog = $modal.open({
            templateUrl : 'security/login/form.tpl.html',
            controller : 'LoginFormController'
        });
        loginDialog.result.then(onLoginDialogClose);
    }

    return {
        showLogin : function() {
            openLoginDialog();
        }
    };

}]);

However, with AngularStrap, I'm struggling to integrate the logic defined in LoginFormController into modals as there's no controller option. Instead, there's a scope parameter, but I'm unsure how to utilize it effectively. Below is a rough idea of how the modal initialization might look:

// AngularStrap version of $modal:

loginDialog = $modal({
    template : 'security/login/form.tpl.html',
    scope : /* Inject LoginFormController scope here */
});

For further information, here are the documentation links for Bootstrap UI: http://angular-ui.github.io/bootstrap/#/modal

and for AngularStrap:

Is it feasible to achieve this, or can $modal only be called as a directive in a template with AngularStrap?

Answer №1

In my experience, I have discovered that utilizing the standard ng-controller syntax functions exceptionally well in this scenario.

Instead of specifying the controller from the code itself, incorporate the attribute on the template code root:

<div class="modal" tabindex="-1" role="dialog" ng-controller="MyCustomModalController as ctrl">
...
<button class="btn btn-primary" ng-click="ctrl.ok()" >OK</button>

The key is effectively sharing the data back to the original controller. While Bootstrap UI employs a convenient callback method which does not translate well with angular-strap, the solution lies in using a shared service - as elaborated in detail here: Can one controller call another?

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

Comparing .innerHTML with createElement() | Exploring setAttribute() versus the Direct method*

Someone mentioned that this approach was not considered "proper", but I didn't pay much attention to it until I encountered a run-time error in IE9. Now, I need help converting the code to utilize object properties instead. What is the reason behind i ...

jQuery Ajax error 403 (unlike XMLHttpRequest)

Recently, I encountered an issue with two Ajax calls in my code. One of the calls was implemented using XMLHttpRequest and the other one using jQuery. Surprisingly, the first call completed successfully without any errors. However, the second call, which s ...

Header Text unexpectedly breaking out of Div in Mozilla Firefox, but not in Chrome or IE

Need some help with getting the h5 text to show up inside the div with id="text". Any advice would be appreciated! Thanks for taking the time to read this! <div id="footer"> <div id="instagram"> <div id="text"> &l ...

Assigning a value to the ng-model in AngularJs based on the user's selection from a Dropdown Menu

I tried to utilize a drop-down Selection to assign the model of the Search Box based on user selection: ng-model=searchText.$ (For search across Any of the 3 fields) ng-model=searchText.name (For search only on name field) ng-model=searchText.age (For s ...

The Springboot endpoint was successfully submitted through a fetch request, leading to a redirection to

Facing an issue while trying to submit a form to the /policy-holder page using fetch in JavaScript. The endpoint is redirecting to the login page, even though only admins are supposed to login. What could be causing this problem and how can it be resolved? ...

Utilizing window.prototype methods in JavaScript for Angular development

I have been tasked with migrating AngularJS code to Angular. There is a JavaScript function that needs to be integrated into an angular component, and the project is using Angular version 12+. The JavaScript function in question is as follows: (function(wi ...

Struggling with incorporating a button into a ReactJS table

I am trying to display a (view) button in the table, but I encountered the following error: Module parse failed: Unexpected token < in JSON at position 165 while parsing near '...2021", "view": <button type="submit...' You m ...

Angular 2+ enables the creation of dynamic folders and allows for uploading multiple files to the server in a seamless

Can an Angular application be developed similar to FileZilla or Core FTP for uploading files and folders to the server via FTP? I need a way to upload files and folders through the admin panel instead of relying on external applications like FileZilla. ...

Load gallery thumbnails dynamically using JavaScript or jQuery

Currently, I have implemented TN3 gallery in a WordPress website (not as a plugin, but as a jQuery library). While the large images in the gallery load dynamically and do not affect the page load, the thumbnails are all loaded at once, even if they are no ...

Utilizing Angular for fetching Rails ID

I am facing a challenge in my Rails 3.2 app where Angular code needs to call a Rails route and pass the id of the page. Despite embedding the id onto the page using a hidden div, accessing it from Angular to send back to Rails has proven to be difficult. ...

Increase the loading speed of the tooltip when hovering over the Legend of the Doughnut chart

I have a similar implementation to the answer provided in this response, which I will share here for better understanding. When you hover between items in the legend and do so quickly, you may notice that the tooltip on the chart does not always display. ...

Validating Linkedin URLs with JavaScript for Input Fields

I've created a basic form with an input field for a LinkedIn URL. Is there a way to validate that this field only accepts valid LinkedIn URLs? Thank you! ...

Include the await keyword within the .then block

I'm trying to execute an await after receiving a response in the .then callback of my code: const info = new getInfo(this.fetchDetails); info .retrieve() .then((res) => { const details = this.getLatestInfo(res, 'John'); }) .ca ...

Modifying the input box value upon submitting a form

My query involves a form with multiple input fields. One of these inputs is for height, with the units specified in cm/inches. However, my API only accepts values in cm. So, if a user selects inches and submits the form, I need to covert the height value ...

Is it feasible to incorporate an external library as a script within a class or functional component in React?

Welcome and thank you for taking the time to read this question! I am currently working on a project where I need to load a TIFF image. After researching, I found a library that can help with this task: https://github.com/seikichi/tiff.js There is also ...

Currency symbol display option "narrowSymbol" is not compatible with Next.Js 9.4.4 when using Intl.NumberFormat

I am currently utilizing Next.JS version 9.4.4 When attempting to implement the following code: new Intl.NumberFormat('en-GB', { style: 'currency', currency: currency, useGrouping: true, currencyDisplay: 'narrowSymbol'}); I ...

Experiencing difficulty incorporating JSON and JS into jQueryhandleRequestJSON and JS integration with jQuery

I'm having trouble fetching the 'sigla' field from a JSON file and inserting it into an HTML 'option object'. I could use some assistance with this issue, so if anyone out there can lend a hand, it would be much appreciated! Here ...

I am encountering problems with converting Array to JSON format in PHP for utilization in Javascript

I always face challenges when it comes to converting Array into JSON format. Currently, I am utilizing a selectbox plugin developed by TexoTela. For this plugin to work, it requires a specific JSON structure as shown below: { "ajax1": "AJAX option 1 ...

Swapping out a code snippet within an HTML document using JavaScript

As a new member of this community, I present to you my very first problem that needs solving. It might be a piece of cake for some of you, but for me, it's proving to be quite tricky. The task at hand involves replacing or removing a string to make ev ...

"Oops! Looks like there's a reference error - the function you're trying

Javascript: function insertSmiley(a) { var $img = $(a).children('img'); $("#message").insertAtCursor(($("#message").data("wbb").options.bbmode) ? $("#message").data("wbb").toBB($(a)): $(a).html()); return false; } HTML: <a href= ...