|
| 1 | + |
| 2 | +// Import the core angular services. |
| 3 | +import { Component } from "@angular/core"; |
| 4 | +import { NgForm } from "@angular/forms"; |
| 5 | + |
| 6 | +// Import the application components and services. |
| 7 | +import { ReactiveBridgeEvent } from "./form-event-bridge.directive"; |
| 8 | + |
| 9 | +// ----------------------------------------------------------------------------------- // |
| 10 | +// ----------------------------------------------------------------------------------- // |
| 11 | + |
| 12 | +// NOTE: We could have used a ViewQuery() to get the "#ngForm" ref / NgForm directive |
| 13 | +// instance injected into the app-component constructor. This would have given us access |
| 14 | +// to ngForm.control - the underlying FormGroup. But, for the sake of the demo, I am |
| 15 | +// looking to access the FormGroup events via event-bindings in the template. That's |
| 16 | +// where the "reactive bridge" directive comes into play. |
| 17 | +@Component({ |
| 18 | + selector: "my-app", |
| 19 | + styleUrls: [ "./app.component.less" ], |
| 20 | + templateUrl: "./app.component.htm" |
| 21 | +}) |
| 22 | +export class AppComponent { |
| 23 | + |
| 24 | + public form: { |
| 25 | + pet: { |
| 26 | + name: string; |
| 27 | + }; |
| 28 | + }; |
| 29 | + |
| 30 | + // I initialize the app component. |
| 31 | + constructor() { |
| 32 | + |
| 33 | + this.form = { |
| 34 | + pet: { |
| 35 | + name: "Lucy" |
| 36 | + } |
| 37 | + }; |
| 38 | + |
| 39 | + } |
| 40 | + |
| 41 | + // --- |
| 42 | + // PUBLIC METHODS. |
| 43 | + // --- |
| 44 | + |
| 45 | + public handleFormStatusChange( event: ReactiveBridgeEvent ) : void { |
| 46 | + |
| 47 | + console.group( "form - reactiveBridge - (statusChange)" ); |
| 48 | + console.log( event ); |
| 49 | + console.groupEnd(); |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + public handleFormValueChange( event: ReactiveBridgeEvent ) : void { |
| 54 | + |
| 55 | + console.group( "form - reactiveBridge - (valueChange)" ); |
| 56 | + console.log( event ); |
| 57 | + console.groupEnd(); |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + // NOTE: This isn't part of the reactive-bridge. This is just the normal "change" |
| 62 | + // functionality of the NgModel directive. |
| 63 | + public handleModelChange( event: string ) : void { |
| 64 | + |
| 65 | + console.group( "ngModel(ngModelChange)" ); |
| 66 | + console.log( event ); |
| 67 | + console.groupEnd(); |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + public handleModelGroupStatusChange( event: ReactiveBridgeEvent ) : void { |
| 72 | + |
| 73 | + console.group( "ngModelGroup - reactiveBridge - (statusChange)" ); |
| 74 | + console.log( event ); |
| 75 | + console.groupEnd(); |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + public handleModelGroupValueChange( event: ReactiveBridgeEvent ) : void { |
| 80 | + |
| 81 | + console.group( "ngModelGroup - reactiveBridge - (valueChange)" ); |
| 82 | + console.log( event ); |
| 83 | + console.groupEnd(); |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + public handleModelStatusChange( event: ReactiveBridgeEvent ) : void { |
| 88 | + |
| 89 | + console.group( "ngModel - reactiveBridge - (statusChange)" ); |
| 90 | + console.log( event ); |
| 91 | + console.groupEnd(); |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + public handleModelValueChange( event: ReactiveBridgeEvent ) : void { |
| 96 | + |
| 97 | + console.group( "ngModel - reactiveBridge - (valueChange)" ); |
| 98 | + console.log( event ); |
| 99 | + console.groupEnd(); |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + // NOTE: This isn't part of the reactive-bridge. This is just the normal submit |
| 104 | + // functionality of the NgForm directive. |
| 105 | + public handleSubmit( event: NgForm ) : void { |
| 106 | + |
| 107 | + console.group( "ngForm(submit)" ); |
| 108 | + console.log( event ); |
| 109 | + console.log( JSON.stringify( this.form, null, 4 ) ); |
| 110 | + console.groupEnd(); |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments