Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular.js and Require.Js

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

like image 842
Victor Avatar asked May 20 '13 12:05

Victor


People also ask

What is require in AngularJS?

The ng-required Directive in AngularJS is used to specify the required attribute of an HTML element. The input field in the form is required only if the expression inside the ng-required directive returns true.

Is it OK to use JS in angular?

Yes. Angular does use . ts files by default. But if you write simple javascript code in them it will still work.

Should I use require in angular?

No - Don't use require. js OR browserify with Angular. JS there is simply no need to do that - AngularJS has a module system and using another module system above it will make your life unnecessarily hard.


1 Answers

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

like image 117
mirrormx Avatar answered Sep 23 '22 16:09

mirrormx