The output from the Angular .then function is not showing up on the webpage

Within my stucontrollers.j, I have the following code:

/// <reference path="../angular.js" />
var stucontrollers = angular.module("stucontrollers", []);
stucontrollers.controller("GetStudentsList",
    function GetStudentsList($scope, $http) {
    $http({
        method: 'GET',
        url: '/api/students'
    }).then(function(data) {
        $scope.students = data;
    });
});

Additionally, in my view, I've included this:

<div ng-app="StudentApp">
    <div ng-controller="GetStudentsList">
        <table>
            <thead>
                <tr>
                    <th>Id</th>
                    <th>FistName</th>
                    <th>LastName</th>
                    <th>Age</th>
                    <th>Gender  </th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="item in students">
                    <td>{{item.Id}}</td>
                    <td>{{item.FirstName}}</td>
                    <td>{{item.LastName}}</td>
                    <td>{{item.Age}}</td>
                    <td>{{item.Gender}}</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

In my app.js, you'll find the following code:

/// <reference path="../angular.js" />
var module = angular.module("StudentApp", ["stucontrollers"]);

Lastly, my StudentsController.cs has the following snippet:

// GET: api/Students
public IQueryable<Student> GetStudents()
{
    return db.Students;
}

I'm facing an issue where the list of students only appears in the response of the page inspector and not on the actual page. Any suggestions on what could be causing this? Thank you.

UPDATE I can now view my data in the console, however, it still doesn't display on the page. Below is the data displayed in the console:

data
:
Array(2)
0
:
{Id: 1, FirstName: "Juan", LastName: "Dela Cruz", Age: 25, Gender: "Male"}
1
:
{Id: 2, FirstName: "Maria ", LastName: "Makiling", Age: 30, Gender: "Female"}

Answer №1

Make sure to invoke $scope.$apply() after assigning the value.


$http({
  method: 'GET',
  url: '/api/students'
}).then(function(data) {
  $scope.students = data;
  $scope.$apply();
});

Alternatively, you can use:


$http({
  method: 'GET',
  url: '/api/students'
}).then(function(data) {
  $scope.$apply(function() {
    $scope.students = data;
  });
});

Referencing the $apply documentation:

The $apply() function is utilized to run an expression in AngularJS from outside of the AngularJS framework.

Answer №2

If you encounter an issue, consider adjusting the value of $scope.students directly in this manner:

/// <reference path="../angular.js" />
var stucontrollers = angular.module("stucontrollers", []);
stucontrollers.controller("GetStudentsList",
    function GetStudentsList($scope, $http) {
    $scope.students = [{Id: 1, FirstName: "John"}];
});

After making this change, one of two scenarios may occur:

a) If you can see John in the list: it indicates that ng-controller and ng-repeat are functioning properly. The issue might lie within retrieving the data. To troubleshoot further, try outputting your response using console.log.

b) If John is not visible in the list: there could be a problem even before the request is made.

Answer №3

My solution to the problem was successful, you may want to give it a try :

/// <reference path="../angular.js" />
var stucontrollers = angular.module("stucontrollers", []);
stucontrollers.controller("GetStudentsList",
    function GetStudentsList($scope, $http,$rootScope) {
    $http({
        method: 'GET',
        url: '/api/students'
    }).then(function(data) {
    $rootScope.$apply(function(){
        $scope.students = data;
    });
  });
});

Answer №4

Give this a shot:

    /// <reference path="../angular.js" />
    var studentControllers = angular.module("studentControllers", []);
    studentControllers.controller("FetchStudentList",
    function FetchStudentList($scope, $http) {
    $http({
        method: 'GET',
        url: '/api/students'
    }).then(function(response) {
        $scope.students = response.data;
    });
});

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

Transitioning away from bower in the latest 2.15.1 ember-cli update

I have been making changes to my Ember project, specifically moving away from using bower dependencies. After updating ember-cli to version 2.15.1, I transitioned the bower dependencies to package.json. Here is a list of dependencies that were moved: "fon ...

The art of posting with ExpressJS

I'm encountering a problem where the data submitted through a form to my POST route is not getting passed on to a database document, even though the redirection works fine. I'm unsure of how to troubleshoot this issue. blogpost-create.ejs &l ...

What methods can be used to incorporate animation when the display attribute transitions to none?

Is there a way to add animation in a Vue app when replacing elements? I would like the transition from, for example, clicking on a div with 'Num 1' to the divs with class 'showing' not disappear abruptly but smoothly, such as moving to ...

In my Vue project, I am required to extract only the numerical value from a select option text and disregard the rest of the text

Currently, I am in the process of learning Vue and have taken on the task of creating a basic tax calculator. The challenge is to display the result in real-time without requiring a "show total value" button. Everything seems to be functioning well except ...

Disabling the camera feed in a React application

Attempting to turn off the webcam using javaScript in React has been a bit challenging. Whenever I shift to a new component or when a component is destroyed, my react component immediately moves to the next page, but the webcam light remains on in the new ...

Tips for extending the space between one element and another when the width decreases:

Currently in the process of building a website using HTML/CSS/JS and have run into an issue. My front page features an image with text overlay, where the image has 100% width and a fixed height of 487px. I positioned the text using position:relative; and t ...

Having trouble with my loop using Jquery's $.get method

Having an issue with Jquery mobile / JavaScript I'm struggling to properly loop through the different values of data in my "get" function, where it should be sending ...cmd=PC&data=06464ff ...cmd=PC&data=16464ff ...cmd=PC&data=26464ff ...

Steps for updating the nested array object using the unique identifier: "id"

Having trouble updating the nested state array in my module where users scan QR codes to update tracking numbers. Currently, I am using react js for frontend and Laravel for backend. Issue: When scanning the QR code, the tracking number is not being set t ...

Having trouble retrieving data passed between functions

One of my Vue components looks like this: import '../forms/form.js' import '../forms/errors.js' export default{ data(){ return{ form: new NewForm({ email: '&apos ...

What could be the reason for the failure of .simulate("mouseover") in a Jest / Enzyme test?

I have a scenario where a material-ui ListItem triggers the display of a material-ui Popper containing another ListItem on mouse over using the onMouseOver event. While this functionality works as expected, I am facing difficulties replicating the behavior ...

What is the best way to transfer the search query to a table filter when working with multiple JavaScript files?

I am struggling with passing the search query from my search file to my table file. The data for my datagrid table is retrieved from a database using an API call, and the table code is in one file while the search functionality code is in another file. I h ...

Choosing options from an API response in a REACT-JS application

I have a Select Component in my application and I want it to automatically show the selected data once the response is received. import Select from "react-select"; <Select menuPlacement="auto" menuPosition="fixed" ...

Is there a way to search for a specific item within a nested array?

I have 2 arrays within an array, each containing objects. How can I locate the object with the name "Sneijder"? const players = [ [ { id: 1, name: "Hagi", }, { id: 2, name: "Carlos", }, ], [ { id: 3 ...

Failure to trigger Summernote's OnImageUpload function

After transitioning to the latest version of Summernote, which is Version 7, I encountered a problem with the image upload functionality. Despite specifying the line onImageUpload: function(files) {sendFile(files[0]);}, it seems that this code is not being ...

What steps should I take to host a Node.js application on a subdomain using an apache2 VPS?

Currently, I have a Node.js application that I would like to host on a subdomain using my VPS. The VPS is running apache2 and the Node.js app utilizes Express. Despite trying both Phusion and following this tutorial, I have been unsuccessful in getting i ...

Troubleshooting problems with AJAX call in custom validation for Knockout JS

I have created a custom validation function that checks whether a zip code entered in a form field is valid for the selected state from a dropdown. The validation works correctly when I initially select a state and then input a zip code, but if I change th ...

Unexpected issues have arisen with the $.ajax function, causing it

I am facing an issue where I can see the loader.gif but unable to view avaiable.png or not_avaiable.png in my PHP file. What could be causing this problem? $(document).ready(function()//When the dom is ready { $("#inputEmail").change(function() { ...

If the condition is met with the presence of X in the string, execute one action; otherwise, proceed with

Currently, my code successfully grabs the results: module.exports = async function grabResult(page) { const message = await page.$eval( 'div > div:nth-child(2)', (el) => el.innerText ); const username = await page.$eval( ...

What is the process for extracting the time value from a jQuery timepicker?

I have been attempting to retrieve values from my time picker when a selection is made, but I am not seeing any values returned nor displayed in my HTML timepicker input field. Despite trying various methods, none have proven successful thus far. Method ...

Execute a virtual mouse click on a button without the need for a physical click

I'm facing an issue with buttons that have a canvas animation triggered by the react library react-ink. The problem is that this animation only works when the button is clicked using the mouse cursor. However, in my application, I have assigned hotkey ...