Feeding JSON information into an AngularJS Bootstrap popup

Despite my extensive search on S-O, I couldn't find a solution to my particular issue. If this has been asked before, please excuse me.

My goal is to display JSON data in a modal (using UI-Bootstrap), but I'm facing difficulties. Even after trying various examples from online sources, none showed the data.

I've set up a new View for the modal window with the following code:

<div ng-controller="MainCtrl" class="modal-header">
            <h3 class="modal-title">More Info</h3>
        </div>
        <div class="modal-body">
        <p>{{user.username}}</p>
            <ul>
                <li ng-repeat="user in users">
                    <a href="#" ng-click="$event.preventDefault(); selected.item = item">{{ user.username }}</a>
                </li>
            </ul>
            Selected: <b>{{ user.username }}</b>
        </div>
        <div class="modal-footer">
            <button class="btn btn-primary" type="button" ng-click="ok()">OK</button>
            <button class="btn btn-warning" type="button" ng-click="cancel()">Cancel</button>
        </div>

The modal is accessed through the main.html (main view) displayed below:

<md-scroll-shrink><header sticky layout="row" layout-xs="column" class="header">
  <h3 class="text-center affix-top ">User Directory</h3>
</header>
</md-scroll-shrink>


<div layout="row" layout-md="column">


    <div flex>
      <md-card id="card" ng-repeat="user in users">
        <md-card-title>
          <md-card-title-text>
            <span class="md-headline">{{user.name}}</span>
            <span  class="glyphicon glyphicon-phone"></span>
            <span class="md-subhead">{{user.email}}</span>
          </md-card-title-text>
          <md-card-title-media>

          </md-card-title-media>
        </md-card-title>
        <md-card-actions layout="row" layout-align="end center">
        <button  ng-click="open()" class="btn btn-success">More Info</button>
        </md-card-actions>
      </md-card>
    </div>

</div>

Finally, here's the main controller (main.js):

'use strict';

/**
 * @ngdoc function
 * @name peapodTestApp.controller:MainCtrl
 * @description
 * # MainCtrl
 * Controller of the peapodTestApp
 */

angular.module('peapodTestApp')
  .controller('ModalInstanceCtrl',function($scope,$uibModalInstance,UserService){
  $scope.ok = function () {
    $uibModalInstance.close();
  };

  $scope.cancel = function () {
    $uibModalInstance.dismiss('cancel');
  };
  })

  .controller('MainCtrl', function ($scope,UserService,$uibModal,$log) {
    this.awesomeThings = [
      'HTML5 Boilerplate',
      'AngularJS',
      'Karma',
      'ngMaterial'
    ];

    $scope.users = UserService;
    $scope.animationsEnabled = true;

  $scope.open = function (UserService,size) {

    var modalInstance = $uibModal.open({
      animation: $scope.animationsEnabled,
      templateUrl: 'views/modal.html',
      controller: 'ModalInstanceCtrl',
      size: size,
      resolve: {
        user: function () {
          return $scope.user;
        }
      }
    });

    modalInstance.result.then(function (selectedItem) {
      $scope.selected = selectedItem;
    }, function () {
      $log.info('Modal dismissed at: ' + new Date());
    });
  };



  })
  .service('UserService',function(){
    var user = [{ 
    // Inserted dummy JSON data to save space and obsolete lengthy content.
}];

    return user;
  })

By the way, an update on my previous question: I still can't get the modal to show my JSON values despite trying suggested solutions. Any additional advice would be greatly appreciated. Thank you.

UPDATE: Regrettably, I continue to face challenges displaying my JSON data in the modal. I've tested recommended fixes without success. If anyone has further suggestions, please share them. Thank you.

Answer №1

To ensure that the modalInstanceCtrl properly receives the resolved variable, the user should be injected into the controller instead of userService.

.controller('ModalInstanceCtrl',function($scope,$uibModalInstance,user){
    $scope.user = user;

    $scope.ok = function () {
        $uibModalInstance.close();
    };

    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };
})

By following this approach, the resolved user will be successfully injected into the controller for optimal functionality.

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 data segments in Knockoutjs using various models

I'm trying to figure out why Knockout.js won't allow me to access specific parts of the model data. Could it be because I am binding the model to the div that contains all the submodels (such as the Form in this example), or am I approaching this ...

Name of Document (changing from php to ?)

Greetings! Currently, I am utilizing PHP to include the document name as an example. folder/<?phpecho basename(__FILE__, '.' . pathinfo(__FILE__, PATHINFO_EXTENSION));?>_button.png I have observed that using PHP for this purpose and havin ...

Contrasts between the text/plain format and the application/json type

I'm pondering the distinction between text/plain and application/json. Which one is the preferred choice and how should they be properly used? Recently, I made a POST request to API Routes in NextJS without specifying headers. The Content-Type of the ...

Utilize Quasar CSS breakpoints within Vue expressions for responsive design

I am currently using the QDialog component from Quasar Framework and I want to set the value of the maximized property based on the current screen size, specifically being maximized for small screens only. Is there a way for me to reference a variable in ...

Can an Updatepanel control be added to a webpage using Javascript or JQuery?

I'm currently working on a project that involves allowing users to drag icons representing user controls onto a web page. For the desired functionality, these user controls must be contained within an updatepanel (or a similar AJAX-enabled frame) so ...

Currently waiting for the $resolved property of the AngularJs service

Here's a straightforward issue with what I hope is an equally simple solution. I've set up multiple services for handling CRUD operations with tags. myApp.factory('GetTags', ['$resource', function ($resource) { return $re ...

Ordering MySQL based on `field`, parameters 1, 2, 3 from a JSON array

I'm trying to create a MySQL query: select * from `table` order by field(`column`, "param1", "param2", "param3") The parameters param1, param2, param3 are coming from a JSON array ["param1", "param2", "param3"]. When I hardcode these parameters in t ...

How can I save JSON to a file without including escape backslashes in the saved data?

I am currently working on writing code that can generate JSON data and save it to a file without including the backslashes used for escaping quotes. The current output I'm getting looks something like this: "https://api.flickr.com/services/rest/forma ...

"Exploring the intricacies of API request methods in the ME

Currently, I am engrossed in Simon Holmes' book titled 'Getting MEAN with mongo, express, angular and node'. It's a fantastic read so far, but one major question has been nagging at me which the book doesn't seem to address. Holmes ...

Tips for retrieving a nested data value within an array

I am currently puzzled by the undefined error I encounter when attempting to access a value using dot notation. The following illustrates my point: My goal is to retrieve the value from within the nested object in the headline color array: ...

Using JS/PHP to send a POST request to a PHP script for querying a database

Trying to gather user input and then pass it to another PHP script for utilizing in a MariaDB query. Index.php: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="robots" content=&q ...

Sending an array from native IOS to JavaScript involves using a bridge or communication channel to

Currently developing an application that involves sending data from JavaScript to a native iOS controller. Wondering what is the best method for sending an array from the iOS controller to a JavaScript file. Appreciate any guidance, thank you. ...

How can I prevent the angularFire $add(user) method from duplicating records? Is there a way to ensure that firebase records remain unique?

I need help preventing duplicate records in AngularFire when adding users to my database. How can I ensure that a user is only added once, without creating duplicates? Here is an example of the button I am using: <a class="btn btn-block btn-lg btn-suc ...

I am eager to display this JSON data using AngularJS tables

I have JSON file with content about categories, departments, and digital marketing: { "categories":[ { "dept_id":"123", "category_name":"database", "category_discription":"list of database", "current time ...

Is it possible to capture and handle browser-specific errors that occur during an AJAX call? I am having trouble locating and capturing these errors

My goal is to thoroughly test an AJAX call for potential errors by deliberately breaking it in various ways. The error callback in jQuery ajax does not provide detailed information like what the browser logs, which is what I need to capture. For example, C ...

Save user's email and password for future logins after the initial login

Is there a way to automatically populate the user's email and password in the login form when they check the "remember me" option and return to log in again? This is for a project using React and Next.js. ...

JEST - Highlighting the presence of asynchronous methods with yellow lines on the coverage report

Experiencing an issue on Vue JS, specifically with a method within a component: https://i.sstatic.net/yAjr0.png The method loadProducts is async, and running JEST tests reveals that line 320 is not covered, despite all other lines and code within the func ...

I often struggle with creating social media buttons using Material UI

https://i.sstatic.net/jmZKS.jpg https://i.sstatic.net/4RQwe.jpg Unfortunately, the code provided is not rendering social media icons or buttons as expected. In the initial lines, material UI icons/buttons were imported from the React Material UI library ...

What is the best method for changing a string of JSON data into a JSONArray

Is there a way to convert the given string into a JSONArray format? {"result":{"passion":[{"id":2,"description":"Sushi"},{"id":3,"description":"Dinner"},{"id":4,"description":"Sex"},{"id":5,"description":"Boobies"},{"id":6,"description":"Sleep"},{"id":7," ...

How can I display components using Angular CLI 5 without any visual output?

The index.html page is not displaying anything, not even the "App works" message that should appear in a basic initial project ng --version Angular CLI: 1.6.0 Node: 8.9.1 OS: darwin x64 Angular: 5.1.0 ... animations, common, compiler, compiler-cli, core, ...