Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the scope of an Angular module Import

Tags:

angular

If I have three modules

  1. AppModule
  2. SharedModule
  3. ItemModule

Inside the SharedModule I have a PaginatorComponent that is exported as follows:

shared.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { PaginatorComponent } from './paginator/paginator.component';

@NgModule({
  imports: [
      CommonModule
  ],
  declarations: [
      PaginatorComponent
  ],
  providers: [],
  exports: [
      PaginatorComponent
  ]
})
export class SharedModule { }

I then import the shared module into my AppModule using the lines

app.module.ts

import { SharedModule } from './shared/shared.module';

imports: [
      ...
      SharedModule,
      ...

Question; Should my third "ItemModule" automatically have access to the PaginatorComponent or do I need to import directly into the 3rd module? Currently I get a variation of the generic error:

Can't bind to 'totalItems' since it isn't a known property of 'app-paginator'.
1. If 'app-paginator' is an Angular component and it has 'totalItems' input, then verify that it is part of this module.

Many thanks.

like image 207
prime Avatar asked Jul 17 '17 14:07

prime


People also ask

What happens when you import a module Angular?

providers of a bootstrapped module have application scope. Adding a service provider to @NgModule. providers effectively publishes the service to the entire application. When you import an NgModule, Angular adds the module's service providers (the contents of its providers list) to the application root injector.

What is the use of import in Angular?

It serves as a registry (aka container) for all the components, pipes, directives and providers in your application. An import is what you put in the imports property of the @NgModule decorator. It enables an Angular module to use functionality that was defined in another Angular module.

What is the scope of Angular service?

Services are one of fundamental blocks of every Angular application. Service is just a TypeScript class with or even without @Injectable decorator. When you provide the service at the root level, Angular creates a single, shared instance of service and injects into any class that asks for it.

Where can I import Angular modules?

Importing moduleslink When you use these Angular modules, import them in AppModule , or your feature module as appropriate, and list them in the @NgModule imports array. For example, in the basic application generated by the Angular CLI, BrowserModule is the first import at the top of the AppModule , app. module. ts .


2 Answers

This is something I misunderstood when I started Angular 2+/

You need to re-import every component you use in every module.

With you example, if you need to use PaginatorComponent into your ItemModule, you will need to import SharedModule into your ItemModule to make it work. (because SharedModule exports PaginatorComponent)

This is not the case for services/providers. If you inject a service, every child component will be able to access it.

like image 172
Deblaton Jean-Philippe Avatar answered Sep 22 '22 21:09

Deblaton Jean-Philippe


The answer can be found at https://medium.com/@cyrilletuzi/understanding-angular-modules-ngmodule-and-their-scopes-81e4ed6f7407 thanks to @Rama

In short, the answer is

  1. If the module is imported for components, you’ll need to import it in each module needing them;

  2. If the module is imported for services, you’ll need to import it only once, in the first app module.

like image 30
prime Avatar answered Sep 22 '22 21:09

prime