0

I have a an ASP.NET MVC 5 application with AngularJS.

Some pages requires data from server, which I load using $http provider. But it has a big delay while it is waiting for an answer. For example, I return page from the server, on this page I have Foo Angular controller which firstly makes GET request to server and then my data is parsed into table. While this request is performing I can see template html which then results in normal table.

I found that most solutions uses Angular Routing, but in my case it is not a good way, because it is not really a SPA.

I tried this solution, but also without any real good results.

So, is it possible to load data from the server when my controller starts without using Angular Routing?

UPDATE

Here are screens which can show the problem.

When the main page is loaded I see this:

enter image description here

Then, after few some time I can see the resulting page with angular processing:

enter image description here

Original request which performs at this time:

enter image description here

The problem is next: is it possible to not show table layout (first screen) while angular retrieve data from server?

3
  • 1
    Angular Routing is not about loading data - it is about showing different views of you SPA app. You load data exactly as you probably do - with $http. If your question is about the long delay - there is not enough information in the question to answer this. Look into the network traffic in Chrome dev tools - perhaps it takes a long time for the server to reply. Also, perhaps you have lots of data and takes time render... In other, there is not nearly enough information in the question. Commented Mar 23, 2015 at 17:33
  • The first page is shown, not while data is loaded (unless you are preventing angular from bootstrapping), but while angular loads. The answer below about ng-cloak should address it Commented Mar 23, 2015 at 17:53
  • Thank you, ng-cloak is my solution Commented Mar 24, 2015 at 7:42

2 Answers 2

1

Angular has a built in directive meant to handle this situation, ngCloak.

The ngCloak directive is used to prevent the Angular html template from being briefly displayed by the browser in its raw (uncompiled) form while your application is loading. Use this directive to avoid the undesirable flicker effect caused by the html template display.

Just ensure that angular.js is loaded in the head of the document, and add the ng-cloak directive to any HTML elements you are having issue with:

<div ng-cloak>{{ 'hello' }}</div>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Claies. This works great for me. Its awesome
0

Check out the documentation on Bootstrapping: https://docs.angularjs.org/api/ng/function/angular.bootstrap

This basic set up is all you would need.

<div ng-controller="WelcomeController">
  {{greeting}}
</div>

Then in your controller make your service call.

var app = angular.module('demo', [])
.controller('WelcomeController', function($scope, $http) {
      $http({
          method: 'GET',
          url: 'yoururl.com'
      }).success(function(response){
           $scope.welcome = response;
      });
});
 angular.bootstrap(document, ['demo']);

Alternatively, if you're not building a SPA it may be a better option to just use jQuery's AJAX methods.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.