4

I am trying to play Angular2, and when I upgraded the base codes to Angular 2.0.0-RC5, I found a breaking changes introduced, @NgModule, which is equivalent with angular.moudle() in Angularjs 1.x?

For me, @ngModule make things complex.

When I was using Angular 1.x, there are some complaints in communities about angular.module, because there are some javascript module systems existed.

By default, Angular-cli generated codes are based SystemJS, we can use Typescirpt to organize the structure of source codes.

Why we need @NgModule, it is a logic view of the application organization at runtime?

I assume a @NgModule is designated for module based architecture for Angular2, and a @ngModule can be reused in different applications. Compare to other module/bundle/plugin based frameworks I have used in the past years. It lacks some features.

  1. @NgModule provides import/export features, but how to import modules without Typescript import clause, and refuse other @NgModule to import @NgModule private components/pipes/services.
  2. There is no route prefix of a @NgModule, imagine t```he @NgModule is maintained by an 3party team. eg. a common Comment module for any post based application, how to avoid route conflict, how to override the route definitions, how to configure a @ngModule in the host @NgModule?
  3. Why route config is registered by RouteModule.forRoot()(or forChild) not a route attribute of @NgModule annotation like this.

    @NgModule(route:{prefix:'/comment', config: commentRouteConfigs}) export class CommentModule{}

    Currently, the @NgModule API coupled route configs.

  4. How to override the component template defined in a @NgModule when I import a module in a host module.

  5. How to hook @ngModule lifecycle(mout/unmout or load/unload etc, I can not find the desc in Angular docs), and how to share data state between components/pipe/service simply without a service in a @ngModule internally?
  6. If possible to design this @NgModule as an optional feature, or use a implicit @ngModule by default for an application if we do not declare @ngModule explicitly.

Thanks.

3
  • Good question, especially the part how to use a third party module without interfering with each other. I also added a similar question - stackoverflow.com/q/39010439/1303917 - It seems to me that @NgModule is more coupling than decoupling. How can they be used standalone without gathering all the dependencies from @NgModule.
    – Yves
    Aug 18, 2016 at 5:28
  • Just check this Article - angular.io/docs/ts/latest/guide/ngmodule.html - if you don't know about it already. Thanks to @mxii who answered to my question.
    – Yves
    Aug 18, 2016 at 5:46
  • @Yves Nevertheless, I am enjoying Angular now, all of my routing rules are registered in a separate sidecar NgModule against the feature module.
    – Hantsy
    Aug 18, 2017 at 13:46

1 Answer 1

3

As far as I know NgModule was mainly introduced to properly support lazy loading of routes and components by the router.

  1. NgModules import and export is not related to TS import and export. It is mostly about dependency injection and compiling templates.

  2. don't know

  3. don't understand the question

  4. Overriding templates is not supported by Angular2 at all. There are build tools that allow that, but Angular2 itself doesn't provide anything to do that.

  5. I have seen module lifecycle callbacks seen mentioned in discussions but couldn't find anything concrete yet (like https://github.com/angular/angular/issues/10808#issuecomment-239823985)

  6. I guess it's possible but I have no idea if the Angular2 team does consider this.

1
  • I noticed the exciting lazy loading feature in RC5, just confused about the @NgModule design, thanks for your reply.
    – Hantsy
    Aug 17, 2016 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.