Ensure that the view is only updated once the JSON has been completely received from the AJAX call in AngularJS

Just starting out with angularJS and still in the learning phase. I'm currently working on creating a navbar for the header that will fetch data from an ajax call.

The issue I'm facing is that it displays {{obj.pageData}} until the data is fully loaded. This can be quite frustrating to see every time I refresh the page!

Is there a way to prevent the display of {{obj.pageData}} in the view and instead have it updated directly once all the data is fetched from the JSON?

Below is a snippet of the code:

View:

<nav ng-app="topNavApp" ng-controller="navCtrl" class="nav">
  <div class="nav-center">
    <!--<li ng-repeat="obj in pageData.allChildList" ng-model="parentNav" ng-mouseover="parentNav=true" ng-mouseleave="parentNav=false"> -->
    <div ng-repeat="obj in pageData.allChildList" class="hiding-div" ng-mouseover="showDiv()" ng-mouseleave="hideDiv()" > 
      <div>
        <a ng-href="{{obj.pagePath}}" class="main-link multiple menu-link">{{obj.pageTitle}}</a>
        <div class="farm-links" ng-show="hovering">
          <div ng-repeat="child in obj.secondLevelChildList" class="groups-links">
            <a ng-href="{{child.pagePath}}" class="group-title">{{child.pageTitle}}</a>
            <ul ng-repeat="subchild in child.thirdLevelChildList" class="group-links">
              <li class="second-link">
                <a ng-href="{{subchild.pagePath}}">{{subchild.pageTitle}}</a>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</nav>

Controller:

angular.module('topNavApp', []).controller('navCtrl', ['$scope', '$timeout', '$http', function($scope, $timeout, $http){
    $http.get("/content/projdata/myJSONData.json").then(function(response){
        $scope.pageData = response.data;
    })['catch'](function(){
        console.log('failed');
    });
}]);

If you need any further details, feel free to ask.

Answer №1

To avoid the temporary display of the angular html template, you can utilize the angular ng-Clock directive.

<div class="content-wrapper" ng-cloak>

</div>

Alternatively, you can opt for using ng-bind in place of {{}}

Answer №2

To control when the data should be displayed, simply set a condition. You can achieve this by using either ng-show or ng-if.

For example:

<div ng-if="pageData" ng-cloack ng-repeat="obj in pageData.allChildList" class="hiding-div" ng-mouseover="showDiv()" ng-mouseleave="hideDiv()" >
    <!--- ... -->
</div>

In this instance AngularJS assesses the expression ng-if="pageData" and will display the div only if pageData is true.

Additionally, you can utilize ng-cloack to prevent any potential page flickering.

Answer №3

Utilize Angular Resolve to ensure data is rendered only after it has been received.

For more information, visit this stackoverflow page.

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

What could be causing this Ajax request to come back with an error?

I am facing an issue with an AJAX request to a WordPress PHP function. The strange part is that the response is being treated as an error. However, when I check the error log, the responseText contains the requested JSON data, the status is 200, and statu ...

Encountering a JSON parse error while utilizing the getJSON function

First time delving into coding with JavaScript and JSON, encountering an error message when using getJSON: parsererror SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data return window.JSON.parse( data ); Below is my code ...

Is it conceivable that Jquery is failing to load for an Ajax request?

I have been experiencing an issue with my jQuery code not working within Ajax requests. The jQuery plugin I am using is FancyBox which can be found at . When calling the FancyBox plugin directly, everything works perfectly and multiple links load the plugi ...

The Firefox form is experiencing issues when the cursor is set to 'move'

I have an HTML form with a specific code snippet: #stoppage_section .stoppage{ cursor: move; /* fallback if grab cursor is unsupported */ cursor: grab; cursor: -moz-grab; cursor: -webkit-grab; } <div id="st ...

Converting a JSON array stored in a local file to a TypeScript array within an Angular 5 project

I'm currently working on developing a web app using Angular 5. My JSON file has the following structure: [ { "id": 0, "title": "Some title" }, { "id": 1, "title": "Some title" }, ... ] The JSON file is store ...

What is the best way to unselect a single checked checkbox within a multi-select widget?

I am looking for a way to automatically deselect a checked checkbox in a multiselect widget. My current approach involves triggering a click event on the checkbox and then sending an AJAX request to a PHP page to check for some results. If the result is su ...

Leverage the power of Angular to seamlessly integrate jQuery code across your

Interested in incorporating the plugin into my AngularJS project. To achieve this, I have to: $(document).ready( function() { $("html").niceScroll(); } ); The challenge is figuring out where to place this code so that it runs site-wide and ...

Discovering what is impeding the rendering of a webpage

On a particular website, the server rendering happens quickly but there seems to be a delay on the client side which is causing the HTML to be rendered few seconds later. I suspect it could be due to some setTimeout function or similar; are there any tool ...

Combining the Powers of Angular JS and Symfony2

Currently working on a project involving Symfony2 and in need of some advice. Considering a hybrid application approach in two ways: a) Using a traditional form with CSRF Token for the Login Page handled by Symfony2, and b) Inner pages (potentially module ...

You can eliminate the display flex property from the MuiCollapse-wrapper

Having trouble removing the display flex property from the MuiCollapse-wrapper, I did some research and found the rule name for the wrapper in this link https://material-ui.com/api/collapse/ I have been unsuccessful in overwriting the class name wrapper t ...

Is it necessary for an embedded YouTube video to automatically play with sound?

My current project involves developing a Web Application where the client has specified that videos must autoplay with sound when a user visits it. Here is a snippet of what I have implemented: HTML: <embed id="video1" src="" wmode="transparent" type= ...

How to retrieve Angular directive name using TypeScript

I have successfully implemented the following AngularJS directive: export module Directives { export class PasswordsMatch implements ng.IDirective { public static Factory(name: string) : ng.IDirectiveFactory { return () => new ...

Is it possible for an AJAX request to return both HTML data and execute callback functions simultaneously?

Is it possible to update the content of an HTML div and call a JavaScript function with specific parameters obtained through AJAX after the completion of the AJAX request, all within a single AJAX call? ...

PHP Command Line Interface can effectively parse JSON strings provided as arguments

When utilizing PHP 5.3 and passing a JSON encoded string as an argument via command line... /usr/local/bin/php -q /path/to/script.php {"key":"test","cache":1} ... the code within script.php looks like this: <?php print_r($argv); ?> The output is: ...

What is the impact of a floated element on vertical spacing?

I have come across an article discussing the usage of CSS, but I am puzzled as to why image number four is not positioned below image number one. Instead, it appears below image number three. Can someone please explain this to me? Here is the HTML code sni ...

PHP function is not able to receive variables after an ajax request

In the process of developing a search function, I created a method that retrieves data from a database, stores each advertisement in a variable, and saves pagination information in another variable. This allows the data to be returned as an array for later ...

Using VueJS: Passing a variable with interpolation as a parameter

Is there a way to pass the index of the v-for loop as a parameter in my removeTask function? I'm looking for suggestions on how to achieve this. <ol class="list-group"> <li v-for="task in tasks" class="list-group-item"> ...

Interactive website information

Currently, my website features a jQuery UI navigation system and utilizes ajax to dynamically update content by swapping div ids from one html file to another. However, this method still involves multiple HTML files which can become cumbersome. I am curiou ...

Attempting to modify read-only properties is prohibited in strict mode within the context of [background: url({{XXX}}) no-repeat center center

I encountered an issue in Edge, but everything works fine in Chrome. I can't figure out what's causing the problem... <div class="container-fluid project_img" style="background: url({{_project.images.web}}) no-repeat center center;"> ...

Customizing CSS based on URL or parent-child relationship in WordPress

I'm having trouble finding a solution for what seems like a simple issue... Currently, my header has a border-bottom and I'd like to change the color of this border based on the section the user is in. For example, consider these parent pages: ...