----app.module.ts-----
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { routing,appRoutingProviders } from './app.routing';
import {InitModule} from './init';
@NgModule({
imports: [ BrowserModule,routing,InitModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ],
providers: [
appRoutingProviders
]
})
export class AppModule { }
-----kit.module.ts------
import { NgModule,ModuleWithProviders } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import {FormComponent} from './';
@NgModule({
imports: [ CommonModule, FormsModule ],
declarations: [ FormComponent ],
exports: [ CommonModule, FormsModule,FormComponent ]
})
export class KitModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: KitModule
};
}
}
-----FormComponent----
import {Component, Input, OnInit,Optional} from "@angular/core";
import { FormControl, FormGroup, Validators, REACTIVE_FORM_DIRECTIVES } from '@angular/forms';
import {FormConfig} from "./form.config";
import {FormService} from "./form.service";
@Component({
selector: 'keyiis-form',
template: require('./form.component.html'),
styles: [require('./form.component.css')],
directives: [REACTIVE_FORM_DIRECTIVES]
})
export class FormComponent implements OnInit {
@Input() config: FormConfig;
form: FormGroup;
constructor(@Optional() private formService:FormService) {
if(formService) this.config=formService.formConfig;
}
ngOnInit() {
}
}
-------CreateAdminComponent (FormComponent consumers)
import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import {FormControl, Validators} from '@angular/forms';
import { UserService } from '../service';
import { Account } from '../model';
import { FormComponent, FormConfig } from '../kit';
@Component({
selector: 'create-admin',
template: `
<keyiis-form #form [config]="formCfg"></keyiis-form>
`,
styles: [``]
})
export class CreateAdminComponent implements OnInit {
formCfg: FormConfig;
@ViewChild('form', { read: FormComponent }) private formComponent: FormComponent;
constructor(private userService: UserService,public router: Router) {
this.formCfg = {......};
}
ngOnInit() {
}
}
--------InitModule ()------------
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { initRouting } from './init.routing';
import { CreateAdminComponent } from './create-admin.component';
import { KitModule } from '../kit';
@NgModule({
imports: [
CommonModule,
initRouting,
KitModule
],
declarations: [
CreateAdminComponent
]
})
export class InitModule { }
----------initRouting ---------
import { Routes, RouterModule } from '@angular/router';
import { CreateAdminComponent } from './create-admin.component';
const initRoutes: Routes = [
{ path: 'create-admin', component: CreateAdminComponent }
];
export const initRouting = RouterModule.forChild(initRoutes);