You should create a React component for your angular application using AngularLazyComponent
.
The AngularLazyComponent
uses the manifest to lazy load dependencies and bootstrap the angular app.
You should register the new react component using ModuleRegistry.registerComponent()
.
class MyNgComp extends AngularLazyComponent {
constructor(props) {
super(props, {
/*see manifest below*/
});
}
}
ModuleRegistry.registerComponent('Prefix.componentName', () => MyNgComp);
props
contains the parameters from the host and will be available within the manifest.
files
: Array offile entry
and sub arrays offile entry
.file entry
url string or object of type { url : string, optional: boolean }, when optional set totrue
the component will keep loading even when that resource failed to load, by defaultfalse
.
Every url string in the main array will be loaded independently.
Using a sub array allows to serialize the download of its items.
resolve
(optional): A function (() => Promise<object>
) to fetch data before bootstrap and in parallel of downloading thefiles
.prepare
(optional): A function (() => Promise<any> || void
) to prepare data before bootstrap and after allfiles
were downloaded andresolve
resolved.module
: The name of the angular module that will be bootstrapped.component
: The name of your angular application's root directive/component that should be rendered.unloadStylesOnDestroy
(optional, default true): Specifies if loaded stylesheets should be unloaded when component is destroyed.
- The
resolve
function must return apromise
. Common usage forresolve
would be to fetch data that affects how your app is rendered, like experiments or user privileges. - The
prepare
function can return a new promise if asynchronous behaviour is required.
Before being rendered, all of the required files
will be downloaded and resolve
will executed.
Once all files
are loaded and resolve
resolved, the function prepare
will be executed.
Once prepare
finished/resolved angular.bootstrap()
will be called with the component and module you passed.
{
files: ['y.js', `${props.files.fakeFile}`, ['1.js', '2.js', '3.js'], 'z.js'],
resolve: () => {
return fetchExperiments().then(response => {
return {
experiments: response.data // experiments would be available on the props service
};
});
},
prepare: () => {
// customLogic();
// or
// return new Promise(...);
},
module: 'your-module-name'
component: 'your-main-component-name'
}
Your angular application can use a service called props
which contains all the parameters passed from the host.
Once any of the props values are changed, $digest()
will be called for you.
class MyCompController {
constructor(props) {
this.value = 'angular-input-value';
this.props = props;
}
}
myApp.component('myComp', {
template:
`<div>
<div id="value-in-angular">{{$ctrl.props().value}}</div>
<input id="angular-input" ng-model="$ctrl.value" />
</div>`,
controller: MyCompController
});
This props
parameter is an angular services
that can be injects and if called as a function can return props
object.
See {{$ctrl.props().value}}
line in the example above.
Use the routerLink
directive, example:
<router-link to="contacts/inner-route">name</router-link>
Use the moduleRegistry
directive, example:
<module-registry component="moduleName" props="{value: 123}"></module-registry>
All lazy components fire 3 lifecycle events (Via the ModuleRegistry):
reactModuleContainer.componentStartLoading
fires before the scripts are loaded, and before theprepare
function is calledreactModuleContainer.componentReady
fires after the scripts are loaded, theprepare
promise resolved, and the component is on the stage.reactModuleContainer.componentWillUnmount
fire before the component is removed from the DOM.