Fetching data from a nested HTTP request in AngularJS using scope variables

I am encountering an issue with accessing a scope variable that is defined in a nested $http.get function. The code snippet below pertains to my controller (which is related to a partial $routeProvider).

    //Content controller
app.controller('Content', ['$scope', '$routeParams', '$http', function($scope, $routeParams, $http) {
    $http.get('wp-json/wp/v2/posts?slug=' + $routeParams.slug).success(function(res) {
        $scope.post = res[0];
        document.querySelector('title').innerHTML =  res[0].title.rendered + ' | Going Solo ';
        console.log(res);
        $http.get('wp-json/wp/v2/posts?filter[artists]=' + $scope.post.pure_taxonomies.artists[0].slug + '&exclude=' + $scope.post.id).success(function(res) {
            if (res.length > 1) {
                $scope.related = res;               
            }
            else {
                $scope.related = res[0];                
            }
            console.log(res);
        }); 
    }).error(function(res, status) {
        if (status === 404) {
            $scope.is404 = true;
            document.querySelector('title').innerHTML = 'Page not found | Going Solo';
            $scope.errorMessage = 'Error: ' + res[0].message;
        }
    });
}]);

Essentially, I aim to fetch all the songs associated with the one displayed in my content controller (the first $http.get request). To link these songs, I utilize a custom taxonomy known as "artists". To achieve this asynchronously, I perform a new $http.get request within the initial one. In this request, I filter for the current post's taxonomy slug ($scope.post.pure_taxonomies.artists[0].slug) and add a filter to exclude the post itself by including its ID. The data retrieved from these requests appears correct based on the console log.

The challenge arises when attempting to access the second $http.get request in my partials. When using the ng-repeat directive:

<div ng-repeat="songrel in related">
    <div ng-bind-html="songrel.title.extended"></div>
</div>

No content is displayed. It seems that it does not recognize data.related, resulting in the cycle not being entered. What could be causing this issue?

Answer №1

The reason why data.related cannot be found is due to the absence of a declared $scope.data variable. It is recommended to use just related, like this:

<div ng-repeat="songrel in related">
    <div ng-bind-html="songrel.title.extended"></div>
</div>

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

Tips for utilizing a CSS pseudo-element as a jQuery UI handle

When I implement absolutely positioned pseudo elements on an HTML list, they display correctly. However, when I try to make the list sortable, the pseudo element is considered part of the draggable element. How can I clearly indicate that the pseudo eleme ...

Most effective method for concealing parent item title in react js when no grandchild items are present

From a graphql query, I have the following data: resources { publications { books { book 1 book 2 } brochures { brochure 1 brochure 2 } } } I want to format this data ...

Using JavaScript to Transmit URL

Imagine I have a URL similar to this: http://localhost:8000/intranet/users/view?user_id=8823 All I aim to achieve is to extract the value from the URL using JavaScript, specifically the user_id (which is 8823 in this instance), and transmit it through an ...

Which symbol is preferable to use in JS imports for Vue.js/Nuxt.js - the @ symbol or the ~ symbol?

I am seeking guidance on a matter that I have not been able to find a clear answer to. Webapck typically uses the ~ symbol as an alias for the root directory. However, I have noticed that some developers use the @ symbol when importing modules using ES6 s ...

Activate divs with Bootstrap5 modal toggle functionality

What adjustments must be made to the Bootstrap 5 example below in order to achieve the following two objectives: The "afterAcceptingTerms" division should remain hidden until the user clicks on the Accept Terms modal button, ensuring that only the "before ...

``How can I divide a Meteor application into separate entities - a Meteor server handling GraphQL and a ReactJS frontend - that operate independently of

Currently, I have an existing app built with Meteor/ReactJS and GraphQL. My goal is to separate the Meteor server and ReactJS frontend so that they can run independently. I am looking for any examples or references on how to accomplish this and link them ...

Having trouble with AngularJS ngRoute not displaying pages on ng-view?

I've been searching through various questions similar to mine, but I can't seem to find the solution here. For some reason, ng-view is not loading my pages. Am I overlooking something obvious? Here's my code in App.js: var app = angular.m ...

Conceal certain components when a user is authenticated

Below is the content of my app.component.html: <nav class="navbar navbar-expand-lg navbar-light bg-light"> <div class='container'> <ul class="nav navbar-nav"> <li class='nav-item'> <a clas ...

Ensure that errors are checked when the function returns a promise

I recently encountered an issue with a function called doSomething(), which returns a promise chain using the Q framework. The function is structured like this: loadDataSet : function (params) { return Q.fcall(function() { //Do Something ...

How can I manage the hover event for all markers that are overlapping in the same position using the Google Maps API?

I have implemented Google Maps API with Angular in my application. The layout consists of a map on the left side with multiple markers and a table on the right side displaying information about each marker. When hovering over a marker on the map, the corre ...

Using $q.when to simulate AJAX calls through an httpBackend mock with ES6 Promises

When attempting to simulate a response to a JSONP GET request using a function that returns an ES6 promise wrapped in $q.when(), everything seems to be working fine. However, during unit testing, the request isn't intercepted by $httpBackend and goes ...

Injecting styles dynamically into an Angular component enhances its appearance and functionality

I am in need of help with styling a component using CSS styles specified by an object of selectors to CSS properties. Check out my sandbox here: https://codesandbox.io/s/happy-goldberg-4740z Here are the styles to be applied from the parent: const styles ...

"Is there a way to switch out div2 with div1 in JavaScript or jQuery while keeping them in the exact same position as div

How can I swap two div elements in HTML by clicking a button, replacing the current div with another div at the same position? .team-intro { margin-top:38%; height:250px; background-color:#7BD2FF; /*background-image:url('images/backgrounds/down ...

Angular - images load smoothly when running locally, but encounter issues on the development server

I am facing an issue with loading images in my application. I have an <img> tag that loads an image using the planeringskartaSrc variable. Everything works perfectly fine when I run the application on localhost, where the arrayBuffer image is passed ...

Using AngularJS to Dynamically Set the Default Selection in a SELECT Element

In my code using JADE syntax, I have the following structure: select(ng-model="eventTypeUI") option(ng-repeat="c in eventUI", ng-value='c.value', ng-disabled='selectEventCanNotBeUsed(c.value)') {{c.name}} ...

adding content to div is becoming less speedy

Currently, I'm developing a drawing board using only html/css/jquery and the drawing functionality is working smoothly. The approach I've taken involves capturing the mousemove events and placing a dot (div) at each point where the event occurs, ...

Adding JavaScript files to a project in Ionic2 with Angular2 integration

I'm looking to incorporate jQuery into my Ionic2 app, which requires loading several JavaScript files: <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script type="text/j ...

Generate a separate div in an HTML file for each item listed in a JSON document

I have a JSON file structured like this: { "text": "HelloWorld", "id&quo;t: "1", }, { "text": "HelloMoon", "id": "2", } Now, I want to generate a <div> in my HTML for ...

What is the best way to conceal the blackberry cursor in a web browser?

Is there a way to conceal the blackberry cursor while browsing on the browser? Perhaps through Javascript or CSS? I am attempting to replicate the functionality found in native apps where users can flick through items with their finger. I find this featur ...

What is the best way to incorporate animations for the initial page load?

https://i.sstatic.net/ZwSww.pngIs there a way to add animations during the initial page load in Next Js? I would like to have a series of blocks displayed in gray with a loading animation before the actual content appears. Are there any libraries availab ...