6

I am studying Angular.js and Require.js at the moment. I encountered an issue here while I am using this two together. For example in my html I have something very simple like {{3+3}}. I found out this will only be evaluated to 6 few seconds after everything is loaded. The user can clearly see the whole process. But this is not good for me. I do not want the delay here. Can someone explain why this happened? Is this because I am doing manually bootstrap.

This is my main.js for require and my bootstrap code.

require.config({
  paths: {
    jquery: 'lib/jquery/jquery-1.9.1.min',
    bootstrap: 'lib/bootstrap/bootstrap.min',
    angular: 'lib/angular/angular.min'
  },
  baseUrl: '/Scripts',
  shim: {
    'angular': { 'exports': 'angular' },
    'bootstrap': { deps: ['jquery'] }
  },
  priority: [
    'angular'
  ]
});

require(['angular', 'app/admin/app.admin', 'app/admin/app.admin.routes', 'app/admin/index/index', 'app/admin/app.admin.directives'], function (angular, app) {
  'use strict';
  angular.element(document).ready(function () {
    angular.bootstrap(document, ['app.admin']);
  });
});

Cheers

1 Answer 1

6

As you're loading some dependencies with require.js before bootstraping the angular, user have to wait for all those files to be completely loaded before application starts. And because HTML is loaded first before your scripts, it's raw content is made visible to the user for a while.

If you don't wan't the user to see the content of your HTML element before it's processed by angular, use the ngCloak directive which is made just for this. Take a look at this entry of the documentation: http://docs.angularjs.org/api/ng.directive:ngCloak. This directive sets the element's style so that it's initially hidden and when angular bootstraps and finishes processing the HTML element's style is set back to visible, thus only showing the compiled HTML.

Update:

But as the above solution won't work straightaway in your case (because angular library script would have to be loaded before the template body — be included in document's head), you should additionally include the following styling in the head section of your document:

[ng\:cloak], [ng-cloak], [data-ng-cloak], [x-ng-cloak], .ng-cloak, .x-ng-cloak
{
    display: none;
}

This is what angular appends to page styles after being loaded.

Yet another solution would be not to insert the {{ }} bindings directly inside elements' bodies, but using ng-bind, ng-bind-template and ng-bind-html-unsafe directives instead. You can find their descriptions here:

http://docs.angularjs.org/api/ng.directive:ngBind http://docs.angularjs.org/api/ng.directive:ngBindTemplate http://docs.angularjs.org/api/ng.directive:ngBindHtmlUnsafe

Sign up to request clarification or add additional context in comments.

3 Comments

Not working so good. I think this is because ng-cloak will only work after the application being bootstrapped.
As described in this answer, you must define a display:none in your styles to initialise any ng-cloak'ed elements as hidden until Angular has chance to digest them after bootstrap. When Angular has processed them it then makes the element visible, preventing unbound expressions from being displayed before Angular has a chance to do it's thing.
The "Update" section is very helpful. The ngCloak documentation mentions the that ngCloak CSS is injected in angular.js, but I hadn't thought to just replicate these classes in the HTML page's <head> section.

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.