GithubHelp home page GithubHelp logo

angular-require's Introduction

angular-require

use require in angular painless

Requirements

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

Usage

  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'])
.config(...)

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([
        'controllers/demoCtrl',
        'services/demoService'
      ]),
      css: $requireProvider.requireCSS([
        'css!css/main.css',
        'css!css/demo.css'
      ])
    }
});

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([
        'resolves/data1',
        'resolves/data2'
      ])
    }
});

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

define(['app'], function(){
  return ['$q', '$timeout', function($q, $timeout){
    var deferred = $q.defer();
    $timeout(function(){
      deferred.resolve('data1');
    }, 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

License

MIT

angular-require's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.