use require in angular painless
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


use require in angular painless


  • require.js
  • require-css.js (optional, if you will load css files, you should include it)


  1. load angular-require.js before your app's init function. You can load it using require.js or just write in html.
  2. include ngRequire in your angular app's dependence.
  3. Done.

Ater doing above, you can use require to load your modules and dependencies asynchronous.

angular.module('app', ['ngRequire', 'ui.router'])

What's provided by ngRequire

You can use $requireProvider in configBlocks. $requireProvider have four methods.

  • require
  • requireJS
  • requireCSS
  • requireResolve

The first three methods are the same actually. You can use them to require JS, CSS or any files.

$stateProvider.state('demo', {
    url: '/demo',
    templateUrl: '/path/to/demo.html',
    controller: 'demoCtrl',
    resolve: {
      deps: $requireProvider.requireJS([
      css: $requireProvider.requireCSS([

You can also just list modules's dependencies when define the module like below. Then require.js will handle them. Compose you have controllers/demoCtrl.js file.

define(['app', 'services/demoService'], function(app){
  app.controller('demoCtrl', ['$scope', 'demoService', function($scope, demoService){
    // demoService is available here!

requireResolve is the method you can use to take full use of $stateProvider's resolve option. If you want to prepare necessary data before the controller injected in order to prevent page from flicking, you can use this method.

$stateProvider.state('demo', {
    url: '/demo',
    templateUrl: '/path/to/demo.html',
    controller: 'demoCtrl',
    resolve: {
      // ... your controllers, services, css and something else can be required here
      data: $requireProvider.requireResolve([

In your resolves/data1, you can define the function like this.

define(['app'], function(){
  return ['$q', '$timeout', function($q, $timeout){
    var deferred = $q.defer();
    }, 1000);
    return deferred.promise;

Events provided by ngRequire

If require load files error, it will fire event timeout, nodefine or scripterror. So you can listener these event in angular $rootScope.

All events

  • requireError, in the callback, require.js' error object will be passed in. You can get error.requireType and error.requireModules
  • requireTimeout, in the callback, require.js' error.requireModules will be passed in
  • requireNodefine, in the callback, require.js' error.requireModules will be passed in
  • requireScripterror, in the callback, require.js' error.requireModules will be passed in