diff --git a/apps/parent-form/src/app/app.component.spec.ts b/apps/parent-form/src/app/app.component.spec.ts index 447d5b1..f1f5bd6 100644 --- a/apps/parent-form/src/app/app.component.spec.ts +++ b/apps/parent-form/src/app/app.component.spec.ts @@ -5,11 +5,12 @@ import { AppComponent } from './app.component'; import { RouterTestingModule } from '@angular/router/testing'; import { ReactiveFormsModule } from '@angular/forms'; import { NameComponent } from './name/name.component'; +import { ContactModule } from '@nested-forms/contact'; describe('AppComponent', () => { beforeEach(async(() => { TestBed.configureTestingModule({ - imports: [RouterTestingModule, ReactiveFormsModule], + imports: [RouterTestingModule, ReactiveFormsModule, ContactModule.forRoot()], declarations: [AppComponent, NameComponent, AddressListComponent, AddressComponent] }).compileComponents(); })); diff --git a/apps/parent-form/src/app/app.component.ts b/apps/parent-form/src/app/app.component.ts index 0fd1911..d1bf59b 100644 --- a/apps/parent-form/src/app/app.component.ts +++ b/apps/parent-form/src/app/app.component.ts @@ -1,5 +1,4 @@ -import { ContactService } from './../../../../libs/contact/src/lib/contact.service'; -import { Contact } from '@nested-forms/contact'; +import { Contact, ContactService } from '@nested-forms/contact'; import { Component, OnInit, OnDestroy } from '@angular/core'; import { FormGroup, FormBuilder } from '@angular/forms'; import { Subscription } from 'rxjs'; diff --git a/apps/parent-form/src/app/app.module.ts b/apps/parent-form/src/app/app.module.ts index c9cef15..9b8002b 100644 --- a/apps/parent-form/src/app/app.module.ts +++ b/apps/parent-form/src/app/app.module.ts @@ -7,6 +7,7 @@ import { ReactiveFormsModule } from '@angular/forms'; import { NameComponent } from './name/name.component'; import { AddressListComponent } from './address-list/address-list.component'; import { AddressComponent } from './address/address.component'; +import { ContactModule } from '@nested-forms/contact'; @NgModule({ declarations: [AppComponent, NameComponent, AddressListComponent, AddressComponent], @@ -14,6 +15,7 @@ import { AddressComponent } from './address/address.component'; BrowserModule, RouterModule.forRoot([], { initialNavigation: 'enabled' }), ReactiveFormsModule, + ContactModule.forRoot(), ], providers: [], bootstrap: [AppComponent] diff --git a/libs/contact/src/index.ts b/libs/contact/src/index.ts index 25a1343..e623161 100644 --- a/libs/contact/src/index.ts +++ b/libs/contact/src/index.ts @@ -1,2 +1,3 @@ export * from './lib/contact-model'; export * from './lib/contact.service'; +export * from './lib/contact.module'; \ No newline at end of file diff --git a/libs/contact/src/lib/contact.module.ts b/libs/contact/src/lib/contact.module.ts new file mode 100644 index 0000000..bdb780d --- /dev/null +++ b/libs/contact/src/lib/contact.module.ts @@ -0,0 +1,28 @@ +import { NgModule, Optional, SkipSelf } from '@angular/core'; +import { CommonModule } from '@angular/common'; +import { ContactService } from './contact.service'; + +@NgModule({ + declarations: [], + imports: [ + CommonModule + ] +}) +export class ContactModule { + constructor( + @Optional() + @SkipSelf() + parentModule: ContactModule + ) { + if (parentModule) { + throw new Error('ContactModule is already loaded. Import it in the AppModule only'); + } + } + + static forRoot() { + return { + ngModule: ContactModule, + providers: [ ContactService ] + } + } +} diff --git a/libs/contact/src/lib/contact.service.ts b/libs/contact/src/lib/contact.service.ts index b056d42..a2f6fe4 100644 --- a/libs/contact/src/lib/contact.service.ts +++ b/libs/contact/src/lib/contact.service.ts @@ -27,9 +27,7 @@ const contact: Contact = { ], }; -@Injectable({ - providedIn: 'root' -}) +@Injectable() export class ContactService { constructor() { }