The data in the partial view is not being properly shown by using ng-repeat

Welcome to my code snippets!

            
        var app = angular.module("AppModule", ["ngRoute"]);

        app.factory("DataSharing", function () {  
            return { value: 0 }  
        });  

        // Setting up Routing  
        app.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
            debugger;
            $routeProvider.when('/ShowAll',
                {
                    templateUrl: 'Views/ShowAll',
                    controller: 'ShowAllController'
                });
            $routeProvider.otherwise({
                redirectTo: '/'
            });
        }]);
    

This is where my Services are defined:


        app.service("DataService", function ($http) {
            this.getData = function () {
                return $http.get("/api/Data");
            };
            this.getItem = function (id) {
                return $http.get("/api/Data" + id);
            }; 
        });
    

Let's take a look at the ShowAllController:


        app.Controller('ShowAllController', function ($scope, DataService) {
            loadAll();
            function loadAll() {
                var promiseGet = DataService.getData();
                promiseGet.success(function (data) {
                    $scope.Items = data;
                },
                function (errordata) {
                    $scope.error = errordata;
                });
            }
        });
    

Now, in my main index page index.html:


        @{
            ViewBag.Title = "API's";
        }
        @section scripts{
            <script src="~/Scripts/angular.js"></script>
            <script src="~/Scripts/angular-route.min.js"></script>
            <script src="~/Scripts/AppModule.js"></script>
            <script src="~/Scripts/Services.js"></script>
            <script src="~/Scripts/ShowAllController.js"></script>
        }
        <div class="container" data-ng-app="AppModule">
            <div class="panel panel-default">
                <div class="panel-header">
                    <div class="row">
                        <div><h4 class="col-xs-6">GET api/Data</h4><a href="#/ShowAll" class="btn btn-success col-xs-6" role="button">Items List</a></div>
                    </div></div>
                <div class="panel-body" data-ng-view></div>
            </div>
        

And here is the content of my partial view (ShowAll.html):


        <div data-ng-controller="ShowAllController">
            <table>
                <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Age</th>
                    <th>Address</th>
                </tr>
                <tr data-ng-repeat="item in Items">
                    <td>{{item.Id}}</td>
                    <td>{{item.Name}}</td>
                    <td>{{item.Age}}</td>
                    <td>{{item.Address}}</td>
                </tr>
            </table>
        </div>
    

If you encounter any issues with displaying the data, check the HomeController and the ShowAll ActionResult. Don't forget that this is for a web API project.

Thank you for taking a look at my code snippets!

Answer №1

To ensure Angular is aware of the scope var in Config time, add the following statement to your ShowAllController before the ajax call:

app.Controller('ShowAllController', function ($scope, SampleService) {

    $scope.Samples = []; // <- INSERT NEW LINE HERE!

    loadAll();
    function loadAll() {
        var promiseGet = SampleService.getSamples();
        promiseGet.success(function (data) { 
            $scope.Samples = data;          
        },
        function (errordata) {
            $scope.error = errordata;
        }
        );
    }
});

If Angular Dependency Injection does not set up the scope var Samples in Config time, it may not recognize it as something that needs to be updated during Run time.

If the above solution does not work, consider wrapping the success result of your promise with $scope.$apply(), as Angular might not be aware of it when executing its digest function.

$scope.$apply(function () {
    $scope.Samples = 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

Issues encountered when selecting table data for filtering

One issue I am facing is with my HTML table that contains a lot of data. I have created a select option to filter the table and display the filtered data. The select options are based on the "route" column, with only three options available: Marikina, Mont ...

Unable to use href attribute as intended

HTML: <a id="Switch">Click here to switch</a> <a href="image1_large.png" class="mainA blade"> <img id="mainImage" src="image1.png"/></a> Javascript: <script> $(function() { $('#Switch').click(functio ...

Steps to refresh my View following an Ajax Post request

Working in an MVC environment, I find myself calling a Controller method from my View. The Controller method conducts some validation checks and then returns to the same view with a faulty modelstate. However, despite expecting all of my validation fields ...

What are the best strategies for managing a collection of service requests in AngularJS?

I require assistance with Angular JS. When clicking on a button, I need to make 400 service calls in an iPad hybrid application. However, this process is taking too long. Can anyone suggest how to optimize the service calls? Currently, I am using: forEa ...

Knockout.JS encounters difficulty retrieving the value attribute in a select tag when the data is sourced from the server

After making an ajax request and binding the dropdown list, I noticed that the select tag was not reading the value attribute. However, when I bind the drop down list in the view model, it does recognize the value. For example: It works if I bind the mode ...

Tips for adding a "return" button to a page loaded with AJAX

While working with jQuery in prototype to load pages using Ajax, I've come across a feature on big sites like Facebook and Twitter that I'd like to implement: a 'back' button that takes the user back to the previous page when clicked. S ...

Typescript counterpart of a collection of key-value pairs with string keys and string values

Within the API I'm currently working with, the response utilizes a data type of List<KeyValuePair<string, string>> in C#. The structure appears as shown below: "MetaData": [ { "key": "Name", &q ...

Tips for retrieving the HTML file of a modified canvas in HTML5

I’m currently working on a project to develop a website that allows users to design their own pages by simply dragging and dropping elements onto the canvas. The goal is for users to be able to save their creations as an HTML file. I’m facing challen ...

What is causing the data to be altered outside of the directive scope?

I am having trouble understanding why the data is changing outside the directive in the external div. I expected it to behave the same as in the controller because the external Div is not wrapped in a directive or controller. Therefore, it should remain un ...

Tips for structuring commands in Discord.js

I'm in the process of restructuring my bot's commands. I currently have a folder called commands with all my commands inside, but I want to make it more organized by categorizing them into moderators, fun, and global like this: commands > mo ...

Make an axios request multiple times equal to the number of items in the previous response

In my project, I am using the axios library to convert addresses into their respective coordinates. First, I fetch a list of addresses from an API. Next, I take the RESPONSE object and use Google API to convert each address to coordinates. Finally, I wan ...

Tips for utilizing jquery.load for fetching a section of an html document or an entire html file

I'm experimenting with jquery's load function to dynamically fetch and display HTML content, rather than using $.ajax(). I enjoy exploring the various features that jQuery offers and want to understand how each one works. Below is the code I am ...

Give a radio button some class

<input id="radio1" type="radio" name="rgroup" value="1" > <label for="radio1"><span><span></span></span>1</label> <input id="radio2" type="radio" name="rgroup" value="2" > <label for="radio2"><span ...

Is it possible for me to design a unique path without relying on redux?

I am working on a Loginscreen.js component import React, { Component } from 'react'; import Login from './Login'; class Loginscreen extends Component { constructor(props){ super(props); this.state={ username:'&apo ...

Exploring the iteration of JSON objects within an array using AngularJS

My auto search module has the following JSON structure. I need to iterate through an array of JSON objects and use keys and values as required. I have attempted the code below. However, with the provided JSON object, I am able to retrieve the key but not ...

Creating a unique filter that combines and filters data from two separate API calls for

In my current scenario, I am making two different API calls using Axios in my application. The first call fetches a complete JSON file that populates a table, while the second call retrieves only categories. This setup is due to the complexity of the app, ...

Create a copy of a div element once the value of a select element has

After modifying a select option, I'm attempting to replicate a div similar to the example shown in this link: http://jsfiddle.net/ranell/mN6nm/5/ However, instead of my expected lists, I am seeing [object]. Any suggestions on how to resolve this issue ...

Combine arrays using union or intersection to generate a new array

Seeking a solution in Angular 7 for a problem involving the creation of a function that operates on two arrays of objects. The goal is to generate a third array based on the first and second arrays. The structure of the third array closely resembles the f ...

Attempting to send numerous identifiers in an API request

I encountered a problem while working on a function in Angular that involves pulling data from an API. My goal is to enhance a current segment to accommodate multiple IDs, but I face difficulties when attempting to retrieve more than one ID for the API que ...

Separate a set of HTML elements with specific heights into individual page divisions

The current method in place is quite average: Prior knowledge of each object's height is required. We possess a set of such objects that could potentially be separated by a page break. We start with a blank slate and insert an opening <div class= ...