ngimport
ngimport copied to clipboard
Module.run() will never be run unless that module is included/required in your AngularJS app
In your section "Using this technique to wrap your own legacy modules" you make it seem as if module('myModule').run
is going to be executed with just the code that is shown but that's not the case in my experience. Your myModule
must be bootstrapped in order for that run method to be executed. So this means it must be bootstrapped directly or added as a requires of another module. Whether that be your top-level angular module that's being bootstrapped or one of that module's required
downstream modules.
Example: If I were going to do this to import a contract-service in a react component somewhere in my page, I'd need to go find an angularjs module which will be bootstrapped and tack on contract service as a dependency. Once done, the run
function will get executed on the module and it'll expose my contract-service appropriately.
import ContractService from '../../contract-service.js';
var angular = require('angular');
module.exports = angular.module('a.module.which.will.be.bootstrapped', [
ContractService
])
This unfortunately reduces the flexibility of this technique.
- You can't for instance NPM install a bunch of your AngularJS services and use them in a ReactJS app that isn't bootstrapping any angular.
- You have to have a higher up angularjs module add/require all of the potential modules in which you may at some point want to import using this technique. This seems bad to me as you're either going to include a bunch of modules that may never even be used. Or you'll have to maintain a list of requirements in an angularjs module and include each thing you wish to use in your reactjs code. It just feels messy.