Skip to content

angular2react method can be simplified #29

Open
@cozmy

Description

@cozmy

I want to start by saying that I really like what you've accomplished here. It's a really useful tool for those which want to gradually migrate an AngularJS project to the newer world.

I found that the angular2react can be a bit hard to use and thus I've created a wrapper function which simplifies the API so my team members would use it more comfortably. As you'll see, we don't need to pass the $injector and register the angular component ourselves. It's all done automatically

Here it is:

import { IComponentOptions, module } from 'angular';
import { angular2react } from 'angular2react';

// All converted components have to be registered, thus we create our own module in which we'll register the original ng components.
const ngModule = module('converted-ng-components', []);

let $injector;
ngModule.run(['$injector', $i => $injector = $i]);

export function myAngular2React<Props extends {}>(name: string, options: IComponentOptions) {
  ngModule.component(name, options);

  return angular2react<Props>(name, options, {
    // angular2react requires the $injector but doesn't actually use it until a component needs to be rendered.
    get get() {
      return $injector.get;
    }
  } as any);
}

export const convertedNgComponentsModuleName = ngModule.name;

If you think this approach is fine, I can make a pull request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions