AngularJS will first compile and then load the page

My issue is that when the page loads, it displays AngularJS variables in plain text for a few seconds until Angular compiles. Is there a way to show a loader or just keep it blank until Angular compiles?

Below is my HTML code:

<li ng-repeat="x in notys" class="item js-item">
                <a href="/project/<% x.project_id %>"  class="notification-link">
                    <div class="details">
                        <!--SERVER SIDE-->
                        <span ng-if="x.type == 'AnswerProject'" class="title">New answer added <i>"<% x.subject %>"</i> for <b><% x.body %></b> group, click to check your project.</span>
                        <span ng-if="x.type == 'QuestionProject'" class="title">New question/s added for <b> <% x.subject %> </b> group, check your project <b> <% x.body %> </b></span>

                        <span ng-if="x.type == 'GroupProject'" class="title">New group created: <b> <% x.subject %> </b>. New project assigned to you <b> <% x.body %> </b></span>
                        <span ng-if="x.type == 'GroupProjectUpdate'" class="title">Group <b> <% x.subject %> </b> updated, for <b> <% x.body %> </b> project.</span>

                        <span ng-if="x.type == 'NewComment'" class="title">User <b> <% x.subject %> </b> commented: <b> <% x.body %> </b> in some of your projects. Click to check.</span>

                        <!--CLIENT SIDE-->
                        <span ng-if="x.type == 'NewCommentClient'" class="title">User <b> <% x.name %> </b> commented: <b> <% x.text %> </b> in some of your projects. Click to check.</span>

                        <span ng-if="x.type == 'GroupProjectClient'" class="title">New group created: <b> <% x.name %> </b>. New project assigned to you <b> <% x.project_name %> </b></span>
                        <span ng-if="x.type == 'GroupProjectUpdateClient'" class="title">Group <b> <% x.name %> </b> updated, for <b> <% x.project_name %> </b> project.</span>

                        <span ng-if="x.type == 'AnswerProjectClient'" class="title">New answer added <i>"<% x.answer %>"</i> for <b><% x.groupName %></b> group, click to check your project.</span>
                        <span ng-if="x.type == 'QuestionProjectClient'" class="title">New question/s added for <b> <% x.gName %> </b> group, check your project <b> <% x.name %> </b></span>

                        <p><time-ago from-time="<% x.created_at %>"></time-ago></p>
                    </div>
                </a>
            </li>

Answer №1

One way AngularJS handles uncompiled content is through the use of a directive called ng-cloak

Learn more about ng-cloak here.

You can apply ng-cloak to a div or element in the DOM to prevent any elements beneath it from being displayed before compilation

<div ng-cloak>
</div>

Answer №2

The issue you are facing is that the text appears on the page before all resources are loaded. To resolve this, I recommend using CSS to hide the HTML tag initially and then manually bootstrap Angular after everything has been loaded. Here's a sample code snippet:

// Remove the ng-app tag from the HTML to disable automatic bootstrapping

var app = angular.module('someapp', []),
    htmlNode = document.getElementsByTagName('html')[0];

// Wait for the document to be ready - similar to jQuery
angular.element(document).ready(function(){

    // Manually bootstrap the element
    angular.bootstrap(angular.element(), ['gts-frontEnd']);

    // Display the HTML element again
    htmlNode.style.display = 'block';
});

For more information, refer to the official documentation.

Answer №3

When it comes to your specific use case, there are various approaches you can take. One simple way would be to implement a solution similar to the following:

.controller('MainCtrl', function ( $scope, myService ) {
  $scope.loading = true;
  myService.get().then( function ( response ) {
    $scope.items = response.data;
  }, function ( response ) {
    // Handle the error in some way
  }).finally(function() {
    // This code runs regardless of success or failure
    $scope.loading = false;
  });
});
Then, you can respond to it in your template:

<div class="spinner" ng-show="loading"></div>
<div ng-repeat="item in items>{{item.name}}</div>

source: Displaying Spinner GIF while making an $http request in Angular

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

Loading content from another webpage within a webpage using Ajax

I successfully implemented an Ajax chat in a PHP page and it was working perfectly. However, when I tried to integrate the chat into another page using a pop-up div (via CSS with z-index), I encountered some issues. Here is the code snippet: function sho ...

Determining when a text area has selected text without constant checking

class MarkdownEditor extends React.Component { constructor(props) { super(props); this.timer = null; this.startIndex = null; this.endIndex = null; } componentDidMount() { this.timer = setInterval(() => { this.setSelectio ...

Ensuring contact form accuracy using jQuery and PHP

Can't seem to figure out why I am always getting false from the PHP file even though my contact form validation with jQuery and PHP is working fine. Let me explain with my code: Here is the HTML: <form method='post'> <label& ...

ReactJS popover element - property pushing and event management

I'm currently developing a reactjs application, and I'm working on creating a modular popup component. The goal is to have the button appear as a combination of a badge and an icon, triggering the popup menu on hover rather than on click. Here i ...

Various CSS regulations for various languages/modes in CodeMirror

Can CodeMirror highlight different languages separately in multi-mode? .cm-s-default .cm-string-html {color: blue;} .cm-s-default .cm-string {color: #170;} Usually, CodeMirror applies the same class names for strings, comments, and other objects in all l ...

I'm facing an issue with Angular2 where I am unable to include a component from

I recently downloaded an angular component from npmjs called ng2-easy-table (I actually created this component, so there may have been some mistakes in its development). package.json { "name": "untitled", "version": "1.0.0", "description": "", "m ...

Ways to test the initial launch of an Android application using Cordova

I am developing an Android application using Cordova. My app consists of multiple pages, with the main homepage being index.html. I need to determine if it is the first time a user lands on the homepage after opening the app, regardless of how many times ...

A guide on incorporating router links within a list component in a React project

In one of my projects, I've implemented a navbar with a profile icon that expands to show four different options: "Log in", "Register", "Edit", and "Admin". However, I'm facing an issue where clicking on these links doesn't always redirect m ...

Updating content in AngularJS based on the sidebar can be achieved by following these steps:

Yesterday, I posed a question about how to implement multiple views with Angular in order to have a header and sidebar. After receiving some helpful insights, I was able to make progress on creating a header and sidebar for my AngularJS App. You can view ...

Express js is failing to deliver static assets

Hello, I'm having an issue with Express Js. It seems like a beginner problem - static files are not being served properly. const express = require('express'); express() .set('view engine','ejs') .use(express.stat ...

Why does my node-js API's second endpoint not function properly?

Currently working on my first API project. Everything was going smoothly, until I encountered an issue with my second route, app.route('characters/:characterId'). For some reason, none of the endpoints are functioning, while the first route, app. ...

Updating content in Bootstrap modal after form submission

Recently, I developed a navbar component that triggers a modal box when a specific button is clicked. The modal contains a form where users can input data. After filling in all required fields and clicking the 'Add' button, I aim to hide the form ...

Understanding Json data using Jquery

I am currently learning about Jquery, Ajax, and JSON but I am having difficulty with parsing Json data. Despite researching extensively on stackoverflow Parsing JSON objects for HTML table Access / process (nested) objects, arrays or JSON Parse JSON in ...

Problems with passing state parameters in AngularJS UI-Router

I recently set up my configuration file in a simple way $stateProvider .state('app', { 'url': '/app', 'abstract': true, 'templateUrl': 'views/layout/index.htm ...

Creating a Vue Directive in the form of an ES6 class: A step-by-step

Is it possible to make a Vue directive as an ES6 Class? I have been attempting to do so, but it doesn't seem to be working correctly. Here is my code snippet: import { DirectiveOptions } from 'vue'; interface WfmCarriageDirectiveModel { ...

What is the best way to eliminate the click event from a child element?

Can you help me with this issue? I have a navigation bar with an item called "Author" that shows author information in a dropdown when clicked. The problem is that the dropdown is clickable, causing it to disappear. I want it to only show and close within ...

Utilizing Object Assign to reset the state

Here lies the declaration of my current state: export default new Vuex.Store({ state: { items: [ ], user: { isAuthenticated: false, info: { createdAt: '', email: '', firstName: '&a ...

What is the best way to adjust the size of an SVG path when zooming in Highcharts?

Recently, I developed a module that allows users to draw an svg line over a highchart using the Renderer. However, I encountered an issue where the size and position of the drawn line remain static when the user zooms into the highchart. It is essentia ...

Having trouble updating state following a fetch request in React Native

I'm encountering a strange problem when attempting to update the state object value after making a GET request using either fetch or axios (neither are working). I have tried various solutions I found online, but none of them seem to be effective. Be ...

Exploring the implementation of query parameters in Nest.js

I am currently a freshman in the world of Nest.js. Below is an excerpt from my code: @Get('findByFilter/:params') async findByFilter(@Query() query): Promise<Article[]> { } I have utilized Postman to test this specific router. ht ...