Sending arguments to $http get using Angular UI Modal

Whenever a user clicks an icon in my list of details, I want a modal to open and display more data on that specific facet. This functionality is implemented using Angular UI's Bootstrap Directives.

Below is the current implementation of my controller:

controller('orderCtrl', function($scope, $modal, $http){

var orderModalCtrl = function ($scope, $modalInstance, $http, orders){
        $http.get('json/SI1.json').success(function(data){
            $scope.orders = data;
        });         
    };

$http.get('json/current.json').success(function(data){
    $scope.topLevelOverview = data;         
});

$scope.open = function() {
        $('body').css('overflow', 'hidden');
        var modalInstance = $modal.open({
            templateUrl: 'template/modal/orderModal.html',
            windowClass: 'contactModal',
            controller: orderModalCtrl,
            backdrop : 'static',
            resolve:{
                orders: function (){                        
                    return $scope.orders;
                }
            }
        });

        modalInstance.result.then(function (){
            $('body').css('overflow', 'scroll');
        }, function(){
            $('body').css('overflow', 'scroll');
        });
    };
});

The modal is triggered from a button within an ng repeat directive. Here is a snippet of how it looks:

 <div ng-repeat="(key,overview) in order.orderDetails">
  <div class="row" ng-repeat="(key, val) in overview.snippet">
    <div class="span2"><p>{{val.item1}}</p></div>
    <div class="span3"><p>{{val.item2}}</p></div>
    <div class="span3"><p>{{val.qty}}</p></div>
    <div class="span3"><p>{{val.freq | capitalize}}</p></div>
    <div class="span1"><a href="" class="btn button" ng-click="open();">
            <i class="fa fa-file-text fa-2x"></i></a></div>
  </div>
</div>

I am looking to pass an argument that will determine the content of the GET request based on the item clicked by the user (e.g., 101, 102, 103, etc.). To achieve this, I have proposed the following approach:

 <div class="span1"><a href="" class="btn button" ng-click="open(); routeTo({{val.item1}});">
            <i class="fa fa-file-text fa-2x"></i></a></div>

In the controller defined earlier, we have the following code snippet:

 var orderModalCtrl = function ($scope, $modalInstance, $http, orders){
       $scope.routeTo = function(route){                  
    $http.get('json/'+route+'.json').success(function(data){
        $scope.orders = data;
    });         
};
 };

While this solution appears to be functional, I believe there might be a more efficient way to achieve the desired outcome. I have attempted to research and explore resources related to $http and Angular UI for potential alternatives.

Answer №1

I have experience using the dialogService from the old angularUI library, which is quite similar to the modalService. Here's a guide on how to use it:

  1. Start by passing the item ID to the open function:
    <a href="" class="btn button" ng-click="open(val.item1);">
  2. Next, you'll need to modify the open function in your controller:
    $scope.open = function(item) { ... }
  3. When opening the modal, make sure to include the item ID as a property in the object passed to $modal.open, like this: item: item
  4. In the modal controller, retrieve the item using
    var myItem = dialog.options.item;
  5. Lastly, you can make an HTTP request using the retrieved item data

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

Issue: Unable to locate 'child_process' module in Angular-cli

After creating a new application with angular-cli and adding components, I encountered an error when trying to run/build the application without any test cases included. The error message received was quite lengthy: ERROR in ./node_modules/selenium-webdri ...

Combining Three.js with Theatre.js to animate every single keyframe

I recently incorporated the amazing Theatre.js library to add animations to a section of my website. Now I realize I need to include some animation at the beginning as well. However, when attempting to select and move all the keyframes using the studio U ...

Insert a new child element in front of the final child element

Could anyone offer suggestions on how to achieve my expected result for this code? Note: The code runs successfully from the console, but not from the link function. angular.element('ul.dropdown-user li:last').before('<li class="divider ...

Tips for successfully executing queries with Axios within a Vue application

Is there a way to send query parameters using Axios so that I can access them in my backend code using req.query.email? The following approach doesn't seem to be working as expected: this.$http.post(this.$BASE_URL + 'agents/auth/create-password&a ...

Is `activeClassName` not being recognized by React?

I've been trying to figure out what's wrong with this code, but I can't seem to find a solution anywhere online. Can someone please help me? This code works fine in my other application, so I'm not sure why it's causing issues here ...

What are the steps for integrating Angularfire2 into an Angular application?

Trying to integrate Angularfire2 into a fresh Angular project, but encountered an issue while following the official documentation. This is the guide I followed Upon reaching step 7 - Inject AngularFirestore, console errors were displayed: https://i.sst ...

Enabling Multiple Login Feature in Node.js

const handleLogin = async (req, res) => { const User = require("../models/user"); const LoginInfo = require('../models/login_info'); try { const { email, mobile, password, otp } = req.body; const params = []; if(ema ...

Element with adhesive properties alters its color at a particular location

I need the sticky element to change colors at specific points and then revert back to its original color. The color should be orange initially, green on block 2, and orange again on block 3. For the complete code and to address any issues with jQuery, pl ...

JavaScript is having trouble properly concealing or revealing elements on the webpage

I am currently designing a web form for my wife, who works at an insurance agency. She needs a web-based form to collect information for processing insurance quotes. Without access to a server, I need to ensure that all the details inputted by the user can ...

Set an enumerated data type as the key's value in an object structure

Here is an example of my custom Enum: export enum MyCustomEnum { Item1 = 'Item 1', Item2 = 'Item 2', Item3 = 'Item 3', Item4 = 'Item 4', Item5 = 'Item 5', } I am trying to define a type for the f ...

Discovering ways to verify if an array is empty within JSON data using JMESPath?

I am presenting JSON data that looks like this: [ { "id": "i_1", "name": "abc", "address": [ { "city": [ "city1", "city2" ] }, { "city": [ "city1", "city2" ...

ERROR: Unexpected issue occurred with v8::Object::SetInternalField() resulting in an internal field going out of bounds while utilizing node-cache in Node.js

I recently started working with API exports that contain a large amount of data, so I decided to utilize the node-cache in order to speed up the API response time, as it was taking more than 2 minutes to retrieve the data. Being new to this, I came across ...

Include the distribution file from the npm package in the final build

Working on my react-based project, I've integrated the node-poweredup npm package to enhance functionality. This useful library comes in both a nodejs and browser version. To include the browser version in my app, I simply link the script located at j ...

The div functions seem to stop working properly after they have been multiplied

Initially, I use JavaScript to multiply the div but then encounter issues with the function not working properly. function setDoorCount(count) { $('.doors').html(''); for (var i = 0; i < count; i++) { var content = " ...

Adding a mute/unmute button to your web browser's audio player: A quick guide

My code is functioning perfectly, but I am facing an issue with the lack of mute/unmute buttons. Can someone please help me figure out how to add them? <audio src="MUSIC URL" autoplay loop> <p>Your browser does not support the audio elem ...

Guide to activating a reaction following an occurrence in a React component

I have started developing a React application that loads blog posts along with their associated comments. The challenge I am facing is how to trigger a refresh of the comments component and display the new comment immediately after it has been submitted. ...

Issues with triggering onScroll event in Internet Explorer 11

<html> <head> <style> div {`border: 1px solid black; width: 200px; height: 100px; overflow: scroll;` } </style> </head> <body> <p>Experience the magic of scrollbar within ...

Creating a loading indicator in Angular using ui-router to display when transitioning between states

Can I display a loading icon in Angular when switching to another state using ui-sref until the second state is fully loaded? ...

Enhancing a validation form with the 'onblur' event handler

Exploring the realm of JavaScript, I find myself intrigued by the concept of creating a validation form that activates upon clicking out of the input field. Implementing various techniques to integrate this feature into an existing form has been both chall ...

What could be causing the absence of pagination links on my website?

While attempting to adjust the width of the images, I noticed that the navigation pager below (pages 1,2,3) unexpectedly disappeared. As a CSS beginner, I'm unsure why this happened and how to resolve it. The code for the navigation is still present, ...