Another network request within the ng-repeat loop

My Angular application needs to call web services from two different URLs. The first URL abc.com/student/3 provides a list of students, while the second URL abc.com/parent/idofStudent3 gives the parent's first name when passed the student ID.

I have successfully retrieved data from the first URL but am struggling to retrieve data from the second URL by passing the first record data in ng-repeat. Can you provide guidance on how to display the parent name on the webpage?

HTML Page:

<h1 ng-repeat="x in myWelcome">
{{x.firstname}} || {{x.parentId}} </h1>

Instead of displaying the parentId, I want to call another web service to fetch and display the parent name by passing the parentId as a parameter. How can I achieve this within the current setup?

<script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope, $http) {
        $http.get("abc.com/student/3")
            .then(function(response) {
                $scope.myWelcome = response.data;
            });
    });
</script>

First webservice response example:

   {
    "student":[
    {
         "name":"john",
          "parentId": 12,
    "address":"NYC"
},

    {
         "name":"Rohi",
          "parentId": 14,
    "address":"NJ"
},
 ]
 }

Second webservice responses for parentId 12 and 14:

{
"firstName": "Sr. John"

}
{
"firstName": "Sr. Rohi"

}

-------------------------
firstname || parentName
-------------------------
John      ||  Sr. John
Rohi      ||   Sr. Rohi

Answer №1

<script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope, $http) {
         $scope.myGreetings = []
        $http.get("xyz.com/student/5")
            .then(function(response) {
                $scope.StudentInfo = response.data;
          if($scope.StudentInfo){
                  $scope.myGreetings.push($scope.StudentInfo)
                  $http.get("xyz.com/guardian/$scope.StudentInfo.studentId")
                     .then(function(response) {
                       $scope.guardianData = response.data;
                        $scope.myGreetings.push($scope.guardianData)

            });
          }
        });
    });
</script>

Feel free to try out this solution

Answer №2

At this moment, your search for Parent details is focused on the incorrect scope object; the data stored in the myWelcome object only includes information from the initial service call, which provided a list of student details exclusively.

You have the option to create a new scope object and populate it with data from the second service call, which will contain parent details; subsequently, you can utilize that object within an ng-repeat function.

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

Discover additional and subtract options

I am trying to implement a "See more" and "See less" button functionality for my list items inside an unordered list using the code below, but it is not working as intended. $(document).ready(function() { var list = $(".partners__ul li"); var numTo ...

Ways to identify when a chemical reaction has taken place?

I have developed a Discord bot that interacts with an API related to a game server panel. The bot has tasks to start, restart, stop, and kill the server. I want to enable end users to trigger these tasks by reacting to a specific embed posted by the bot. ...

`The logo does not dim when the popup is displayed`

I'm currently experiencing an issue with pop-ups on my page - when they appear, they create a shade over all elements of the page except for my two logos and are displayed with a border around them. Here's what my page looks like before the popu ...

Increased Z-index on an unfamiliar site

I'm in the process of enhancing the Moodle platform at my school with a userscript, and I want to create a sleek UI for it. One feature I want to add is a progress bar that stays fixed at the top of the browser viewport so that it remains visible as ...

Utilize the filtering feature within the Vue select module

I'm struggling to get the select filter to work properly in my Quasar application. When I open the select, there is no list displayed and no value gets selected. Can someone help me understand why it's not working? <q-select v-mo ...

The ajax POST method fails to trigger in a Node.js environment

Having an issue with the POST request. It's necessary for updating info without page reload. Below is my pug code: .tinder--buttons form(id="form1") button#love(type="submit") i.fa.fa ...

Disappearing Cloned Form Fields in jQuery

Hey there! I'm trying to duplicate a section of a form using the code below. But for some reason, the copied fields are only visible for a split-second before they disappear. Can anyone spot any errors that might be causing this strange behavior? jQu ...

VueJS form validation does not account for empty inputs in both fields

One of the challenges I'm facing is generating a form with Vue.js using the input fields below: { name: 'first_name', type: 'text', label: 'First Name', placeholder: 'First Name', ...

I need to retrieve a date value within a function

When using the input type = date, I am trying to retrieve the selected date value in a function. Here is my code: <input style='display:none;' type='date' value='<?php echo date(Y-m-d);?>' name='date' id ...

Struggling with the conundrum of aligning a constantly changing element amid the

I was optimistic about the code I wrote, hoping it would work out in the end. However, it seems that my expectations might not be met. Allow me to provide some context before I pose my question. The animation I have created involves an SVG element resembl ...

Trouble arises when attempting to categorize an array of objects in JavaScript

My array consists of various objects: [{id: 1, name: 'Apple', category: 'Fruit'} {id: 2, name: 'Melon', category: 'Fruit'} {id: 3, name: 'iPhone', category: 'Phone'} {id: 4, name: 'Samsung Ga ...

Declaring module public type definitions for NPM in Typescript: A comprehensive guide

I have recently developed a npm package called observe-object-path, which can be found on GitHub at https://github.com/d6u/observe-object-path. This package is written in Typescript and has a build step that compiles it down to ES5 for compatibility with ...

Challenges regarding OAuth2 and the angular-auth2-oidc Library - Utilizing PKCE Code Flow

As a newcomer to OAuth2 and the angular-auth2-oidc library, I may make some beginner mistakes, so please bear with me. MY GOAL: I aim to have a user click on the login link on the home page, be directed to the provider's site to log in, and then retu ...

Retrieving information from a distant JSONP Entity

My challenge involves working with a JSONP object that generates 3 random numbers on a website. I have embedded the script below in an HTML document to access this JSONP object. <script> var url = 'http://dev.apalfrey.me/workspace/te2006-te2801 ...

PHP fails to retrieve data from a JavaScript Ajax function using the FormData object (specifically, when

I'm facing an issue where my file is not being detected when I send a FormData object using AJAX and PHP code. Both the `$_FILES` array and the `$_POST` array appear to be empty. However, the browser does send the file with the AJAX request. <inpu ...

The issue arises when Angular routing fails to locate the Django template files

I have been working on a project that involves utilizing Django along with AngularJs routing. Within my URL configurations for Angular routing, I have the following code: app.config(function($routeProvider) { $routeProvider .when( ...

What sets apart `Object.merge(...)` from `Object.append(...)` in MooTools?

This question may seem simple at first glance, but upon further inspection, the MooTools documentation for the 'append' and 'merge' methods appears to be identical. Here is the code snippet provided in the documentation: var firstObj ...

Using setInterval() does not automatically fetch JSON data at a specified interval for me

Attempting to constantly update the latitude and longitude coordinates from a dynamic API has proven to be a challenge. Despite utilizing jQuery with a set interval of 1000 milliseconds, the data retrieval from the API does not occur as expected. While i ...

The JQuery loading symbol does not prevent keyboard interactions

I successfully implemented a Jquery busy indicator in my application using the following link: However, I noticed that even though it prevents users from clicking on input elements while loading, I can still navigate to these elements by pressing the tab ...

Experience the mesmerizing motion of a D3.js Bar Chart as it ascends from the bottom to the top. Feel free to

Here is the snippet of code I am working with. Please check the link for the output graph demonstration. [Click here to view the output graph demo][1] (The current animation in the output is from top to bottom) I want to animate the bars from Bottom to ...