"Integrating a controller into a modal view: a step-by

After spending an unhealthy amount of time on this issue, I finally managed to resolve it. Initially, the modal.open function was only darkening the screen without displaying any dialog box. However, by using windowTemplateUrl to override templateUrl, I was able to make the dialog box appear.

(function() {
  angular
    .module('loc8rApp')
    .controller('locationDetailCtrl', locationDetailCtrl);

  locationDetailCtrl.$inject = ['$routeParams', '$uibModal', 'loc8rData'];
 
  function locationDetailCtrl($routeParams, $uibModal, loc8rData) { 
    var vm = this;    
    vm.locationid = $routeParams.locationid;

    loc8rData.locationById(vm.locationid)
      .success(function(data) {
        vm.data = {
          location: data
        }
        vm.pageHeader = {
          
          title: vm.data.location.name
        };
      })
      .error(function(e) {
        console.log(e);
      });

    vm.popupReviewForm = function() {
      var modalInstance = $uibModal.open({

        templateUrl: "/reviewModal/reviewModal.view.html",
         backdrop: true,
        //windowClass: 'show',
        windowTemplateUrl: "/reviewModal/reviewModal.view.html",
        controller: 'reviewModalCtrl as vm',
        //size: 'sm',
        resolve: {
          locationData: function() {
            return {
              locationid: vm.locationid,
              locationName: vm.data.location.name
            };
          }
        }
      });
    };
  }
})();



//modal controller
(function() {
  angular
    .module('loc8rApp')
    .controller('reviewModalCtrl', reviewModalCtrl);

  reviewModalCtrl.$inject = ['$uibModalInstance', 'locationData'];

  function reviewModalCtrl($uibModalInstance, locationData) {
    var vm = this;
    vm.locationData = locationData

    //create vm.modal.cancel() method and use it to call $modalInstance.dismiss method
    vm.modal = {
      cancel: function() {
        $uibModalInstance.dismiss('cancel');
      }
    };
  }
})();
<div class="container modal-content">
  <form id="addReview" name="addReview" role="form" class="form-horizontal">
    <div class="modal-header">
      <button type="button" ng-click="vm.modal.cancel()" class="close"><span aria-hidden="true">x</span><span class="sr-only">Close</span></button>
      <h4 id="myModalLabel" class="modal-title">Add your review for {{ vm.locationData.locationName }}</h4>
    </div>
    <div class="modal-body">
      <div class="form-group">
        <label for="name" class="col-xs-2 col-sm-2 control-label">Name</label>
        <div class="col-xs-10 col-sm-10">
          <input id="name" name="name" required="required" ng-model="vm.formData.name" class="form-control" />
        </div>
      </div>
      <div class="form-group">
        <label for="rating" class="col-xs-10 col-sm-2 control-label">Rating</label>
        <div class="col-xs-12 col-sm-2">
          <select id="rating" name="rating" ng-model="vm.formData.rating">
                        <option>5</option>
                        <option>4</option>
                        <option>3</option>
                        <option>2</option>
                        <option>1</option>
                    </select>
        </div>
      </div>
      <div class="form-group">
        <label for="review" class="col-sm-2 control-label">Review</label>
        <div class="col-sm-10">
          <textarea id="review" name="review" rows="5" required="required" ng-model="vm.formData.reviewText" class="form-control"></textarea>
        </div>
      </div>
    </div>
    <div class="modal-footer">
      <button ng-click="cancel()" type="button" class="btn btn-default">Cancel</button>
      <button type="submit" class="btn btn-primary">Submit</button>
    </div>
  </form>
</div>

Answer №1

To implement this functionality, I set up the following in the rootController of my app:

$rootScope.openMsgModal = function (modalSettings) {
            var defaultSettings = {
                title: "Title",
                msg: "Message",
                icon: "fa-info-circle",
                iconColor: "#c83637",
                clickAction: "ok",
                isCancelVisible: false,
                templateUrl: 'views/templates/popups/alertMessage.html',
                controller: 'ModalAlertController',
                translations: {
                    confirmButton: "common.button_confirm",
                    cancelButton: "common.button_cancel"
                }
            }
            angular.extend(defaultSettings, modalSettings);
            var modalInstance = $uibModal.open({
                templateUrl: defaultSettings.templateUrl,
                controller: defaultSettings.controller,
                size: 'md',
                resolve: {
                    modalSettings: function () {
                        return defaultSettings;

                    }
                }
            });

            return modalInstance;

        };

    }

Then, when I want to use the modal, I include this code snippet:

    var modalInstance = $uibModal.open( {
        animation: true,
        templateUrl: 'views/templates/popups/alertMessage.html',
        controller: 'ModalAlertController',
        resolve: {
            modalSettings: function() {
                angular.extend(defaultSettings, modalSettings);
                return defaultSettings;
            }
        }
    });

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 Next.js client-side JavaScript application experiences unforeseen disruptions without generating any console errors

My server is responsible for constructing HTML from a JSON response: { content: "<div id=\"_next\">...</div>...<script src=\"...\"></script>" } This is how my Node server handles the data: const output = "&l ...

Exploring JSON Data with Mustache Templates

Dealing with a significantly large JSON object that I can't control, I'm struggling to output a list of records (related to people in this case) using Mustache.js. Despite simplifying the complex object into a more manageable one with just the ne ...

Is it feasible to develop an asynchronous function in Node.js that executes concurrently?

My objective is to simultaneously execute the longRunnigTask and quickTask functions: function longRunnigTask() { return new Promise((resolve) => { console.log('longRunnigTask started'); for (let i = 0; i < 999999999; i+ ...

What is the best way to remove empty elements from an Array?

Having an issue with my API post request. If no values are entered in the pricing form fields, I want to send an empty array. I attempted to use the filter method to achieve this but it still sends an array with an empty object (i.e. [{}]) when making the ...

Combining strings and variables in Vue.js with the use of bootstrap-vue syntax

Hey, I'm facing a little syntax hiccup with '="collapse-{{ product.id }}"' in both the b-button and b-collapse. Any ideas on how to properly structure this? Just looking to set up a unique ID that connects the button to the collaps ...

Utilize multiple classes in inline styling within HTML

I'm trying to dynamically apply the "more" CSS class directly to an inline style tag, but I haven't had success with my current approach. Unfortunately, in my situation, I can't create a class. The method is working for <p> tags but no ...

What steps can you take to address Git conflicts within the yarn.lock file?

When numerous branches in a Git project make changes to dependencies and use Yarn, conflicts may arise in the yarn.lock file. Instead of deleting and recreating the yarn.lock file, which could lead to unintended package upgrades, what is the most efficie ...

Traverse an SVG element using JavaScript

I have a beautiful star SVG that I created, and I want to use it instead of styling a span to create floating bubbles in my div. However, I'm facing some challenges in adapting my Javascript loop to incorporate the SVG for creating the bubbles, as opp ...

The toArray function in MongoDB does not support the use of Array push

I'm attempting to loop through all documents within collections, store them in a global variable, but it seems like the push() function is not working and returning an empty array [] inside of toArray(). Any ideas? const mongo = require('mongodb ...

Use yarn to install both devDependencies and dependencies simultaneously

Can yarn be used to install devDependencies and dependencies simultaneously? For instance, if I need to install react as a dependency and webpack as a dev dependency. Typically, I would have to execute two separate commands like shown below: yarn add reac ...

The use of Buffer() is no longer recommended due to concerns regarding both security vulnerabilities and

I'm encountering an issue while trying to run a Discord bot. The code I'm using involves Buffer and it keeps generating errors specifically with this code snippet: const app = express(); app.get("/", (req,res) => { if((new Buffer(req.quer ...

Is there a method available to minimize the size of a local storage list containing strings?

Hey there, I am trying to load a large 2.5MB json file in my browser so that I can use it for some typeAhead functions. Unfortunately, I'm facing an issue with my local storage being constantly full. When using Firefox, I receive the following error ...

Obtain the filepath of the uploaded file

After working on a code to allow users to upload images to the server, I encountered an issue. Here is my setup: The backend of my system is built using Node JS. My main goal is to retrieve the file path of the uploaded image so that I can successfully up ...

Struggling with Mocha, supertest, and passport when testing authenticated routes with JWT authentication

I've been experimenting with testing authenticated routes in Mocha, but I've run into an issue where the user created in the `before` or `beforeEach` hooks doesn't persist. Here's a snippet from my test.js: const should = require(&apo ...

Having issues with Thymeleaf template not functioning properly when using inline JavaScript

I've encountered an issue when attempting to extract my function into a script within HTML. When written as shown below, it works perfectly: <input type="text" id="myInput" onkeypress="return confirm('Are you sure you want to delete ...

"Implementing button toggling in Angular: Learn how to easily hide and display

Whenever the funone button is clicked, I want the funtwo button to appear and hide the funone button. Then, when the funtwo button is clicked, the funone button should reappear while hiding the funtwo button. How can I hide the funtwo button if it already ...

Transfer all image files from Node.js to the frontend

What is the best way to send all image files from my backend nodejs server folder to my Reactjs client? I have set up a website where users can sign in and upload their files. However, I am facing an issue where only one file is visible on the client side, ...

Issue with jQuery's .height() method not updating

Here is the code I am working with: $(function() { $('#team-ch').click(function() { $('#team-camb-hert').height('auto'); }); }); I am trying to change the height of a div when a link is clicked. After adding an alert ...

Choosing one item from an array to showcase in a view [Angular]

I've previously inquired about this issue without success. My current challenge involves a store app created with AngularJS. I am trying to implement a feature where clicking on an item will open it in a separate "item" view, displaying only the info ...

Sharing Codenvy developer environments for collaborative coding on StackOverflow

I wanted to test embedding a Codenvy Developer Environment in my Stack Overflow post. Here's the code snippet I used: <iframe name="factory-iframe" width="100%" height="600px" src="http://ide3.cf.codenvy-stg.com/factory?v=1.2&vcs=git&vcsurl=http%3 ...