What are some ways to delay the loading of the DOM until the object is completely loaded in the $scope?

I've encountered an issue with loading a JSON file that is nearly 19,000 lines long. When minified to a single line, it's about 240k. I'm currently loading this JSON into my app via the index.html file as a global variable:

<script type="text/javascript" src="js/cdjson.js"></script>

var _countries = {
   "country": {
      "USA": {
         "currency": {
            "currencyCode":["USD"],
                "currencyName":["United States Dollar"],
                "currencySymbol":["&#36;"]
         },
         "info": { 
               ...
               ...
         },
         "phone" : {
               ...
         },
         "postal" : {
               ...
         }
      },
      "CAN" : {
         ...
      }
   }
}  

Within the controller, I am assigning it to a $scope variable like so:

$scope.countries = _countries.country ;
. However, the HTML DOM for that controller tries to access the $scope variable before the object is fully loaded, resulting in JS object errors such as
Cannot read property 'country' of undefined
.

Is there a way to prevent the page from rendering until the $scope object is completely loaded? The $scope country object is used in a

<select ng-repeat="countries as country">

Answer №1

It is recommended to load your js files at the bottom of your HTML to avoid this issue. However, if that is not possible, you can add the following code in your controller:

$scope.countries = []
angular.element(document).ready(function () {
    $scope.countries = _countries.country;
});

Also, make sure to include the following code in your HTML:

<select ng-if="countries.length" ng-repeat="countries as country">

Answer №2

Implement a flag to signal the controller when all data has been successfully loaded. Consider using something similar to:

<select ng-if="countries.length" ng-repeat="countries as country">

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 showing data from Ajax responses on an HTML page

In my code, there are two JavaScript functions: The function fetch_items is responsible for returning data in the form of stringvalue. On the other hand, the function extract_items($arr) passes values to the fetch_items function. function fetch_items ...

Having difficulty deleting items with Three.JS

I'm currently using Three.JS to create a plane and add some boxes on top of it. I occasionally need to remove all the boxes. To achieve this, I have attempted using the code snippet below: for (i = 0; i < scene.children.length; i++) { var obje ...

Utilize Bootstrap4 to position content above the navigation icon

I need assistance to modify my code so that it will have the following appearance: I successfully created a navigation bar icon and inserted the logo, but I am struggling to position the data above the navigation icon: My current code: <nav class="n ...

Enhancing visual appearance with customized look control through the use of setAttribute

I have developed a unique custom look-controls feature and I am trying to integrate it into the scene using 'setAttribute(componentName, data)', but I'm unsure about what parameters to include. Any suggestions? Here is my approach: const s ...

What is the best way to set up multiple unique uglify tasks in grunt?

I am facing issues when trying to create multiple Uglify tasks in Grunt. Currently, I have to toggle between commenting and uncommenting a task to get it to work properly. I need assistance in generating two separate minified .js files using this plugin ...

Auto-suggest dropdown menu with pre-populated options

I am looking to implement a feature where users can click on an icon and a dropdown menu will appear, allowing them to search through data. To better illustrate this concept, here is an image: Users will be able to input their quiz names in the first sect ...

Is there a way to redirect focus to an object with a lower z-index when working on overlaying a sequence of transparent divs on a map?

Is it possible to give focus to a parent div that contains a child div without hiding the child div? I am integrating the Google Maps API and need to overlay a grid of transparent divs for inserting information. However, with these divs on top of my map, ...

The issue with displaying Infogram embed code in a React application

Hi there, I'm having an issue with embedding a code from infogram into my react app. Here is the code snippet: <div class="infogram-embed" data-id="861ca70e-552c-4a4f-960a-4c7e7ff62881" data-type="interactive" data-title="Step by Step Charts">& ...

What is the best way to apply distinct styles to two different views in Angular?

Two of my view editmodes are "New" and "edit". I'm attempting to apply unique styles to a paragraph in each mode. Any suggestions on how to achieve this? <p style="margin-left: 165px; margin-top: -23px;">===some messages---</p> ...

What are the steps to effectively set up a provider service in Angular.js?

What are the recommended practices or patterns for configuring a provider within a config block, similar to how $locationProvider and $routeProvider are configured? It is my understanding that only providers can be configured within config blocks. The co ...

Reorder elements using CSS when they do not share the same immediate parent container

I am looking to rearrange the order of some items on my page using JS or CSS. The project I am working on is written with ReactJS. Here is the basic structure: <div class="parent"> <form> <div class="header"></di ...

Building an array using class value types in TypeScript

Here is a sample class structure: export interface ILanguage { shortName: string; fullName: string; } export class Languages { static readonly FRENCH: ILanguage = { shortName: 'fr', fullName: 'FRENCH' }; static readonly DUTCH: I ...

make the text in em font size larger

Incorporating HTML dynamically into a page using JavaScript is shown in the example below. _strInnerHtml += "<em>" + value.cate_name + " | " + value.city_name + "</em>"; _strInnerHtml += "<a href='#' class='activi ...

What is the best way to resize an image within a canvas?

As I work on displaying an image using the cover simulation in canvas, I came across a helpful solution. Currently, the image changes based on screen resolution, but only updates after a page refresh. https://i.sstatic.net/qEL26.gif Is there a way to ac ...

What could be the reason for the peculiar data being returned by Microsoft Graph list subscriptions?

I am currently in the process of configuring webhooks to automatically update my application's database whenever a contact is modified in Outlook. I need to modify my existing subscriptions before they expire, so I decided to retrieve a list of curren ...

Making changes to a specific row in a datatable within a Flask project

I need help with creating an edit function for my data table. I only require front-end and a bit of JS (Ajax) code to send the data. The picture below shows what I am trying to achieve: https://i.sstatic.net/9QDXl.png Here is how my data table looks like: ...

Twice the charm, as the function `$("#id").ajaxStart(...)` is triggered twice within an AJAX request

I am trying to implement the following code: <script language="javascript"> function add(idautomobile,marque,model,couleur,type,puissance,GPS){ $("#notification").ajaxStart(function(){ $(this).empty().append("<center><br/><i ...

What is the reason behind console.log() displaying an array, while typeof returning 'object'?

This question pertains to the outcome of a mongoose find() operation. After running the code console.log('apparently this is an ' + typeof campaign.advertGroups, campaign.advertGroups); The resulting output is as follows: apparently this is an ...

Attempting to showcase information from a JSON file using the ng-repeat directive

I am currently attempting to present data from a JSON object. <table ng-controller="statsCtrl"> <tr ng-repeat="stat in stats"> <td>{{ stat.name }}</td> <td>{{ stat.id }}</td> </tr> </tab ...

TypeScript Compile Error: The property is not available in the 'IStateParamsService' type

My client project heavily utilizes TypeScript. Currently, I am encountering a technical issue. Within my HTML code, I have an anchor tag as shown below: <a class="btn btn-default mrm" ui-sref="course-detail({courseId: '{{c.Id}}'})">Detail ...