Tips on importing JSON data into an AngularJS modal dialog

Can someone help me figure out why I am unable to display JSON values on my modal using AngularJS? I can see the values in the console or alert message, but they won't show up on the modal dialog. Thank you in advance for any assistance.

Checkout my Plnkr.

This is the HTML:

<!doctype html>
<html ng-app="myApp">
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.js"></script>
    <script src="http://angular-ui.github.com/bootstrap/ui-bootstrap-tpls-0.2.0.js"></script>
    <script src="script.js"></script>
    <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
  </head>
  <body>
<div ng-controller="TestCtrl">
    <button ng-click="test()">Test</button>
</div>
  </body>
</html>

This is the script.js:

angular.module('myApp', ['ui.bootstrap']);
function TestCtrl($scope, $dialog, $http){
  $scope.test = function(){
     $http.get('test.json')
        .success(function(data) {
           $dialog.dialog({}).open('test.html');
            $scope.items=data;
            console.log(data);
            alert(JSON.stringify(data));
        });
 }
 }

This is test.html:

<div class="modal-header">
  <h1>Test Modal</h1>
</div>'
<div class="modal-body">
  Test Data:
    <ul ng-repeat="item in items">
    <li>
      {{item.test1}}
      {{item.test2}}
      {{item.test3}}
    </li>
  </ul>
</div>

This is test.json:

[{
    "test1": "test1value",
    "test2": "10",
    "test3": "100"
}]

Answer №1

It appears that the variable you are trying to access in your modal is not visible.

Here's a revised version of your code:

// Updated code snippet

var app = angular.module('myApp', ['ui.bootstrap']);

function TestCtrl($scope, $dialog, $http){
  $scope.test = function(){
    // $dialog.dialog({}).open('modalContent.html');

    $http.get('test.json')
        .success(function(data) {
           $scope.items=data;
           console.log(data);
           alert(JSON.stringify(data));
           $dialog.dialog({
             templateUrl: 'test.html',
              controller: 'dialogCtrl',
              resolve: { items: function () { return $scope.items; } }
           }).open('test.html');
        });
  }

}

app.controller('dialogCtrl', function ($scope, items) {
  $scope.items= items;
});

Answer №2

It seems like the issue lies in the accessibility of your scope variable within the test.html file.

I've made a simple update by changing the variable to rootScope, and now everything is functioning correctly.

Check out the updated Plunker here

Please note that this solution is just a quick fix and not a full implementation. @Karim's answer may provide a more robust solution, so consider it as the correct answer if it meets your needs.

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

Unpacking JSON Objects in Typescript: Working with Private Variables

I have a TypeScript model object called user export class User { constructor( private _name: string, private _email: string ) {} public get name():string { return this._name; } public set name(value:string) { this._name = value; } g ...

Encountering a "focus" error with React-Native-Phone-Input library, where the property is null

For my project, I decided to incorporate the react-native-phone-input library. Everything was going smoothly until I encountered an issue with their focus function. Initially, it worked perfectly fine, but subsequently, when I attempted to input a phone nu ...

React - Image Uploader exclusively accepts images with transparent backgrounds

I need to verify if an image has a transparent background and reject it if it does, but accept it if it doesn't. However, I am facing an issue where the hasAlpha function is not triggering an 'error' alert when the image contains a backgroun ...

The task "default" is not found within your current gulpfile configuration

After running gulp in my console, I encountered the following error: Task 'default' is not in your gulpfile I double-checked my gulpfile and it appears to be correct: var gulp = require('gulp'), LiveServer = require('gulp- ...

React's UseEffect can cause an endless loop of renders, leading to the error message "Maximum update

My Cart component contains an array of Cards. Whenever a user removes a product from the cart, it is removed from both the local storage and the UI. import React, { useState, useEffect } from 'react'; import { Link } from 'react-router-dom&a ...

Guide on making Play framework 2.4.x serialize field with an empty list

In my project with Scala Play! 2.4.x, I'm currently facing an issue with serializing a case class: case class MyEvent( id: String, parentId: Option[ParentRef] = None, stepStatus: String = "undefined", artifacts:Seq[String] = Seq.empty ...

Navigate the JSON object at predetermined intervals, such as in the case of movie subtitles

Apologies if the title is not specific enough, open to any suggestions for improvement. Here's my issue: I have a JSON file (presented here as a JavaScript object) that contains subtitles for a movie. My goal is to display the text exactly as it appea ...

Creating a Node.js server with Express and enabling CORS functionality

To summarize, I am utilizing a viewer like api for dicom files known as cornerstone, in which I connect to the WADO service of dc4chee to retrieve the dicom data. Dcm4chee is running on port 8080, while my Node.js application uses port 3000. My goal is to ...

What could be causing the issue with ng-show in Angular?

When a user clicks on an icon, I want to display a drop-down menu. I attempted to use the ng-show directive for this purpose, but unfortunately, the drop-down menu is not appearing. I created an icon ..., and when clicked, I attempted to open the drop-do ...

Clicking will open the link in both a new tab and the current tab

I'm looking to enable ng click functionality to work in both new tabs and the current tab. The URL is dynamically generated based on a certain condition. Here is the HTML: <a ng-href="{{myPathVariable }} " ng-click=" GoToThemes()" class="shift-l ...

Is there a program available that can efficiently convert or translate JSON objects into TypeScript types or interfaces?

Can anyone recommend a tool that can easily convert a JSON object into a TypeScript type or interface? An example input would be something like this: I'm hoping to paste the JSON object into the tool and receive an output structure similar to: expor ...

Troubleshooting a JSON response issue in CodeIgniter

As a beginner in the codeigniter framework and json, I am attempting to retrieve data from the database upon user clicking on a specific URL. However, I'm encountering an error. This is my script: function fetchUserData(i) { if(i == 1) { ...

Having trouble with the AJAX request for retrieving image paths, parsing the JSON response into a JavaScript array, and attempting to render the images on the page

Struggling to implement a functionality that involves loading images from a PHP array into a JavaScript array using JSON messages and AJAX. The buildImage() function is used to display the first image in the array within the content div, with onclick event ...

Forcing UTF-8 Encoding in JavaScript

I came across this helpful article: While following the advice of many others suggesting to use unescape(encodeURIComponent(srt)), I encountered issues. Attempting to convert the file obtained through XMLHttpRequest did not yield the desired results. Pri ...

Developing a two-dimensional JavaScript array using an AJAX PHP request

I've been working with a MySQL table that stores image data. My goal is to extract this image data and store it in a JavaScript array. The fields I need for the array are "image_ref" and "image_name." To achieve this, I understand that I'll nee ...

Incorporate action icons (such as edit and delete) into a table in React.js using material-ui

Within my existing table, I utilized a library known as react-bootstrap-table-next This library effectively displays data in a table format with values originating from a JSON response Now, my goal is to integrate an Action column that includes options f ...

The hover feature on my website is making the picture flicker

I am experiencing an issue with a map on my website that contains four colored squares. When I hover over one of the squares, the image of the map changes to show the route corresponding to that color. However, this causes the image to shift position and i ...

"Enhance your webpage with a captivating opaque background image using Bootstrap

I'm new to exploring Bootstrap and I am currently experimenting with options for displaying content with a semi-transparent background image. Currently, I am using a "well" but I am open to other suggestions. I have managed to place the image inside t ...

Sending PHP variable to jQuery using POST

To keep track of page referrals, I often use URLs like this: www.example.com/?ref=google Once I've captured the referral as a variable, it looks like this: if ($_GET['ref'] !== "") { $ref = $_GET['ref']; } else { $ref = ...

When using the keyboard to navigate, the aria-label attribute is not being properly recognized

<span class="phone" aria-label="phone number 9 4 9 . 5 5 5 . 1 2 3 4">949.555.1234</span> This code snippet displays a phone number on the webpage. When clicking on the phone number, it reads the aria-label as intended. However, if you navigat ...