single-spa-angularjs is a helper library that helps implement single-spa registered application lifecycle functions (bootstrap, mount and unmount) for for use with AngularJS. Check out the single-spa-angularjs github.

Installation

  1. npm install --save single-spa-angularjs

Note that you can alternatively <script src="https://unpkg.com/single-spa-angularjs"></script> and access the library via the singleSpaAngularjs global variable if that is easier for you.

With a bundler

If you’re using a bundler such as webpack, add the following to your entry file:

  1. import singleSpaAngularJS from 'single-spa-angularjs';
  2. import angular from 'angular';
  3. const ngLifecycles = singleSpaAngularJS({
  4. angular: angular,
  5. mainAngularModule: 'app',
  6. uiRouter: true,
  7. preserveGlobal: false,
  8. template: '<my-component />',
  9. });
  10. export const bootstrap = ngLifecycles.bootstrap;
  11. export const mount = ngLifecycles.mount;
  12. export const unmount = ngLifecycles.unmount;

Without a bundler

If you’re not using a bundler, you’ll need to make your angularjs application a SystemJS module or a global variable. The SystemJS module is preferred, and you can read about it more in the recommended single-spa setup.

As a SystemJS module

Add the following to your AngularJS application. If you’re using gulp/grunt to concatenate files together, just create a new file called single-spa-application.js and make sure it’s included in your final build file.

  1. System.register([], function(_export) {
  2. return {
  3. execute: function() {
  4. _export(singleSpaAngularJS({
  5. angular: angular,
  6. mainAngularModule: 'app',
  7. uiRouter: true,
  8. preserveGlobal: false,
  9. template: '<my-component />',
  10. }))
  11. }
  12. }
  13. })

Once you do this, you can System.import() the bundle file and SystemJS + single-spa will know what to do with your module. Your loading function should be System.import('name-of-app'). Make sure to add name-of-app to your import map.

As a global variable

  1. window.myAngularApp = singleSpaAngularJS({
  2. angular: angular,
  3. mainAngularModule: 'app',
  4. uiRouter: true,
  5. preserveGlobal: false,
  6. template: '<my-component />',
  7. })

Your loading function should just be the global variable itself. For example:

  1. singleSpa.registerApplication('my-angular-app', myAngularApp, () => true);

Options

All options are passed to single-spa-angularjs via the opts parameter when calling singleSpaAngularJS(opts). The following options are available:

  • angular: (required) The main angular object, which is generally either exposed onto the window or is available via require('angular') or import angular from 'angular'.
  • domElementGetter: (optional) A function that takes in no arguments and returns a DOMElement. This dom element is where the angular application will be bootstrapped, mounted, and unmounted. If not provided, the default is to create a div and append it to document.body.
  • mainAngularModule: (required) A string that is the name of the angular module that will be bootstrapped by angular. See angular docs for angular.bootstrap().
  • uiRouter: (optional) If you are using angular-ui-router, set this option to either true or to a string value. The string value will be the value of the ui-view html attribute. For example, uiRouter: 'core' will be <div ui-view="core" /> whereas uiRouter: true turns into <div ui-view />.
  • preserveGlobal: (optional) A boolean that defaults to false. Set if you want to keep angular on the global even after an app unmounts.
  • elementId: (optional) A string which will be used to identify the element appended to the DOM and bootstrapped by Angular.
  • strictDi: (optional - part of the bootstrap config object) A boolean that defaults to false. Set if you want to enable StrictDi mode
  • template: (optional) An html string that will be inserted into the DOM when the app is mounted. The template goes inside of the element returned by domElementGetter. If not provided, no template will be inserted. When using angular-ui-router, you often do not need to use this since ui-router will be putting a template onto the dom for you.

ES5 Example

Check out this example repo