Attempting to access JSON with AngularJS http.get() causes controller malfunction

Recently I've been diving into the world of AngularJS, and I encountered an issue while trying to parse a json file using the $http method. Initially, when testing a simple variable like $scope.test = "working";, everything seemed to be functioning correctly. However, as soon as I integrated the $http method into my code, both the variable and the data retrieval stopped working. Here is a snippet of the json content:

{
   "programmes":[
      {
         "@attributes":{
            "id":"1"
         },
         "name":"Arrow",
         "imagepath":"..\/img\/arrow.jpg"
      },
      {
         "@attributes":{
            "id":"2"
         },
         "name":"Fargo",
         "imagepath":"..\/img\/fargo.jpg"
      },
      {
         "@attributes":{
            "id":"3"
         },
         "name":"You, me and the Apocalypse",
         "imagepath":"..\/img\/youme.jpg"
      },
      {
         "@attributes":{
            "id":"4"
         },
         "name":"Heat",
         "imagepath":"..\/img\/heat.jpg"
      },
      {
         "@attributes":{
            "id":"5"
         },
         "name":"The Thick of It",
         "imagepath":"..\/img\/thick.jpg"
      }
   ]
}

Additionally, here is a portion of the controller file in question:

app.controller('MainController', ['$scope', function($scope) {
    $scope.test = "works";

    $http.get("../../jsons/programmes.json").success(function(response) {$scope.programmes = response.programmes;});

}]);

Lastly, this is how the html section looks like:

<ul>
  <li ng-repeat="x in programmes">
    {{ x.name }}
  </li>
</ul>

<p>{{test}}</p>

Upon adding the $http.get() call in the controller, the text displayed by {{test}} appears exactly as written, instead of the expected value. Furthermore, the ng-repeat directive within the ul element fails to work altogether. Any ideas on what I might be doing incorrectly?

Answer №1

Make sure you include the $http angular service in your controller. Check your browser console for clues on what might be missing.

app.controller('MainController', ['$scope', '$http', function ($scope, $http) {

If you're seeing {{test}} in your angular expression, it's likely because a dependency is not correctly injected in your controller code.

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 with clicking a button in Selenium using JavaScript for automation

I'm encountering an issue where Selenium is detecting an element as disabled, despite it being enabled. To work around this, I am attempting to click on the element using JavaScript with the following code snippet: IWebElement button = driver.FindEl ...

Using an Android application to connect to an external database

I am currently working on integrating an external database (000webhost) into my app to fetch email addresses and display them in a ListView. While I am receiving the correct response from the server and can see it in the logcat, I am encountering a null po ...

Retrieving geoJSON Markers via API in React Leaflet

I'm working on incorporating Leaflet and utilizing GeoJSON in my project. I am facing an issue where I am unable to render markers from a fetched GeoJSON API onto the map, even after storing the data on the state. I have attempted to use the marker co ...

Timer Tool: JavaScript Countdown Application

I am currently working on a JavaScript countdown timer, but I need help with setting it to only run for 48 hours every time the page is submitted. Right now, when I enter a time, the timer counts down to that specified time. Can someone please assist me ...

Learning the best practices for incorporating publish and subscribe in Meteor JS is crucial for achieving efficient

I need some guidance on my implementation of publish and subscribe in Meteor JS. I am new to this and seeking help to ensure that I am doing it correctly. If you require more information about my code, I am happy to provide additional source code. Despit ...

CSS/JS Label Positioner using Mootools, perhaps?

I have been tasked with incorporating a form into our website. It seems simple at first, but this particular form has some interesting JavaScript code in place to ensure that the label for each input field sits inside it. This is a clever feature, but unfo ...

NextJS middleware API receives an uploaded image file form, but the request is undefined

Currently, I'm utilizing NextJS to handle form data processing and database uploads, with a pit stop at the NextJS API middleware for image editing. pages/uploadImage.tsx This is the client-side code handler. ... async function handleImageUpload(imag ...

Eradicating Pinpointers on Navigation Tool (Google Maps)

I have a feature that utilizes an ajax request to generate a marker or multiple markers when the user interacts with the map. Once a marker is created at a specific location by the user, I then set up a click event on the marker itself. The issue arises w ...

Unable to retrieve the value entered in the md-input field

I am facing an issue with my md-dialog in which the model of the input field is coming up as undefined on the controller even after clicking the top up button. <md-dialog aria-label="Top Up User" ng-cloak> <md-dialog-content> <div cla ...

What is the method for specifying a .php page as the html source when using Ext.Panel?

I have a current situation where I manually set the html for an existing panel using a variable in the following way: var htmlContent = '<H1>My Html Page'; htmlContent += '[more html content]]'; var newPanel = new Ext.Panel({ ...

Rejuvenate with Restangular

Need help with Fullcontact service: angular.module('app1App') .service('Fullcontactservice', function Fullcontactservice(Restangular, $http, $q) { // Singleton instantiation in AngularJS var self = this; self.apiKey = ". ...

"Troubleshooting: Vue ChartJS Line Chart fails to show data

Hey there! I'm currently working on integrating Chart.js with the vue-chartjs wrapper to build a Line Chart using data retrieved from my API. The data is being successfully logged to the console without any errors, but for some reason, the Line Chart ...

When utilizing the map function to render an array of objects, React encounters an error

My React application is throwing an error when using the map function. Interestingly, the code works fine in a sandbox environment. Error: ENOSPC: System limit for number of file watchers reached, watch. After creating a new React project, the error dis ...

"Looking to access your Express app in a VM from the internet? Here's how

I have an express app running on a Node.js server hosted on Azure Linux VM, and I am looking to access this website from my personal computer. const express = require('express'); const app = express(); app.listen(3000, () => { console.lo ...

What are some ways to enhance the speed of my canvas animations?

My code generates imagedata and places it in a canvas. However, when I expand the window size, the rendering slows down significantly. Is there a way to optimize this for smooth operation in fullscreen mode, even though it might seem naive? What would be ...

Destroying the entire HTML content results in the invalidation of both the HEAD and BODY tags post-

Upon clicking a button, I execute a script that sends the entire HTML content of a webpage (everything within the <html> tags) to a CGI script for processing. However, when I attempt to replace the existing content with the manipulated result, I enc ...

Trigger an instantaneous update of the masonry grid

My website currently utilizes a script that generates a grid, and the grid elements are automatically adjusted each time the width of the viewport is modified. Although I do not have access to or control over the script since I did not write it myself, I s ...

Having difficulty in animating the upward movement of textbox2

I've got a form that contains 2 buttons and 2 textareas. When the form loads, I want to display only section1 (button, textarea) and the section2 button. Upon clicking the section2 button, my aim is to hide the section1 textarea, reveal the section2 t ...

Facing an obstacle in Angular as I am unable to view my data

How can I bind the model of my controller in the init function and see the data when the init is called? Index.html <!DOCTYPE html> <html ng-app="I-Sign"> <head> <meta http-equiv='X-UA-Compatible' content='IE=edge&apo ...

Measuring the number of end points in a JSON tree

Looking to tally the total number of leaf nodes within a JSON structure, defined as keys with no further child elements. Struggling to find a straightforward solution, I've attempted to write a function. However, it's proving difficult to create ...