|
| 1 | +<p align="center"> |
| 2 | + <img src="./_images-angular-forms-reactivemodeldriven/angular_logo_1.png" alt="Angular logo" title="Angular logo" width="200" /> |
| 3 | +</p> |
| 4 | + |
| 5 | +Angular Reactive Forms (Model driven) |
| 6 | +===================== |
| 7 | + |
| 8 | +Working with existing/cloned/copied Angular App |
| 9 | +--------------------- |
| 10 | +- Clone or Download the project/app from Github or any other sources |
| 11 | +- If using Visual Studio Code / Insiders, open Command panel/terminal from menu: View -> Terminal (shortcut key is `CTRL + BackTick` OR `COMMAND + J`) |
| 12 | +- Go inside the project/app directory, command: `cd _examples-angular-templateDrivenForm OR cd templateDrivenForm` |
| 13 | +- Run command: `npm install` to install project/app dependencies `(node_modules)` |
| 14 | +- To Build and run Angular App, command: `ng serve / npm start` OR `ng serve -o` OR `ng serve --open` |
| 15 | +- To change the port from 4200 to other port - type command: `ng serve --port 5000` |
| 16 | +- To check the application in browser type path/URL: `localhost:4200 / 5000` |
| 17 | + |
| 18 | +1 - Introduction to Reactive Forms (Model-driven) |
| 19 | +===================== |
| 20 | +1.1 Reactive Model Driven Forms - what is it all about? |
| 21 | +--------------------- |
| 22 | +- Angular reactive forms, also known as model-driven forms, offers an easy way to use reactive programming styles-patterns and validations |
| 23 | +- Reactive forms are forms where we write logic, validations, controls in the component's class |
| 24 | +- It is flexible and can be used to handle complex form scenarios and large forms |
| 25 | +- We write more component code and less HTML code which make unit testing easier |
| 26 | + |
| 27 | +1.2. Some important points about Reactive Forms (Model Driven) |
| 28 | +--------------------- |
| 29 | +- Code and Logic resides in the `component class` (Template Driven Forms focus mainly on HTML template) |
| 30 | +- `No Two Way Data Binding` |
| 31 | + - (we need to react to user inputs to update the values, also some inbuilt angular methods are available to update component class) |
| 32 | +- Reactive forms are mainly `used/well suited for complex scenarios`: |
| 33 | + - `Dynamic (On the Fly creation)` form fields |
| 34 | + - Initially only one field, click on add button new forms/fields created dynamically (+ Add Product, + Add Friend list, + Add Permanent & temporary address, etc.) |
| 35 | + - `Custom Validation (Crossfield validations)`- Password & Confirm Password validation, old & new password/pin validation etc. |
| 36 | + - `Dynamic validation` - If subscribed to notification than email field is mandatory, hierarchy/dependency based scenarios, If Married enter spouse details, etc. |
| 37 | +- `Unit test` - As logic is present in component class (Template Driven Forms we cant unit test HTML templates) |
| 38 | + |
| 39 | +1.3. Steps to work with Reactive Model Driven Forms / Things to do with Reactive Model Driven Forms |
| 40 | +--------------------- |
| 41 | +- Create & use new `Angular CLI` generated project |
| 42 | +- Add form HTML template/markup |
| 43 | +- Create a form model by using `FormGroup` and `FormControl` classes |
| 44 | +- Manage form control data/values |
| 45 | +- `FormBuilder` Service (a simpler way to specify/manage form model) |
| 46 | +- `Validation implementation` - Simple, Custom, Cross-field, and Dynamic validations |
| 47 | +- Add `Dynamic form controls` |
| 48 | +- Submit the form data to `server` |
| 49 | + |
| 50 | +2 - Setting up new Angular project |
| 51 | +===================== |
| 52 | +1. First check `angular cli` installed version details on machine by using command at command prompt: `ng -v` or `ng --version` |
| 53 | + |
| 54 | +<p> |
| 55 | + <figure> |
| 56 | + <img src="./_images-angular-forms-reactivemodeldriven/02-01-01-angular-cli.png" alt="Angular CLI version" title="command: `ng -v` or `ng --version` to check angular cli version" width="1000" border="2" /> |
| 57 | + <figcaption> Image - Angular CLI version</figcaption> |
| 58 | + </figure> |
| 59 | +</p> |
| 60 | + |
| 61 | +2. If `angular CLI` not installed/available on machine (no version or error message displayed) then install it by using the command: `npm install -g @angular/cli@latest` |
| 62 | +3. To `update/upgrade angular CLI` to the latest version, use following commands in sequence: |
| 63 | + - command: `npm uninstall -g @angular/cli` |
| 64 | + - command: `npm cache verify or npm cache clean` |
| 65 | + - command: `npm install -g @angular/cli@latest` |
| 66 | +4. Generate/create a new Angular app/project with Angular CLI - for dealing with angular forms with the syntax: `ng new appName` or `ng new project-name`, command: `ng new angular-forms-reactivemodeldriven` (after creation check the newly generated folder structure) |
| 67 | + |
| 68 | +<p> |
| 69 | + <figure> |
| 70 | + <img src="./_images-angular-forms-reactivemodeldriven/02-01-02-angular-project-structure.png" alt="Angular project/app folder structure" title="Angular project/app folder structure" width="500" border="2" /> |
| 71 | + <figcaption> Image - Angular project/app folder structure</figcaption> |
| 72 | + </figure> |
| 73 | +</p> |
| 74 | + |
| 75 | +5. To run/serve application, at command prompt type command: `ng serve` or `ng serve --port 5000` or `ng serve --port 5000 -o` (`--port flag` - to change the port number and `-o or --open flag` - to automatically launch/open app in browser) |
| 76 | +6. Go to the browser and load the application by typing address: `http://localhost:4200/` or `http://localhost:5000/` |
| 77 | +7. Add the `Bootstrap` framework to an application (CSS framework used to make cool/intuitive User Interface and look/feel) |
| 78 | + - Download bootstrap css to local machine from bootstrap website: https://getbootstrap.com/docs/4.1/getting-started/download/ into folder `assets/library/bootstrap.min.css` |
| 79 | + - Include bootstrap in application - index.html under `head` tag - `<link rel="stylesheet" href="./assets/library/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous" />` |
| 80 | + - or you can include a `CDN` path in index.html under head tag |
| 81 | + - or else you can install bootstrap with npm command: `npm install bootstrap` and use it |
| 82 | + |
| 83 | +<p> |
| 84 | + <figure> |
| 85 | + <img src="./_images-angular-forms-reactivemodeldriven/02-01-03-bootstrap-cdn.png" alt="Bootstrap website - installation options" title="Bootstrap website - installation options" width="1000" border="2" /> |
| 86 | + <figcaption> Image - Bootstrap website - installation options</figcaption> |
| 87 | + </figure> |
| 88 | +</p> |
| 89 | + |
| 90 | +8. To verify bootstrap included/working properly in an application, check in Browser fonts, etc changed or not? |
| 91 | + - Also in `app.component.html` just create any simple component like buttons or divs with bootstrap class: |
| 92 | + - `<button class="btn btn-success">Success Button</button>` or |
| 93 | + - `<div class="lead">Lead Heading</div>` |
| 94 | + - Right click on element and check in `inspect element` the bootstrap class and properties applied to respective elements |
| 95 | + |
| 96 | +> **Syntax & Example**: index.html |
| 97 | +```html |
| 98 | +<!doctype html> |
| 99 | +<html lang="en"> |
| 100 | + |
| 101 | + <head> |
| 102 | + <meta charset="utf-8"> |
| 103 | + <title>ReactiveModelDrivenForms</title> |
| 104 | + <base href="/"> |
| 105 | + |
| 106 | + <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 107 | + <link rel="icon" type="image/x-icon" href="favicon.ico"> |
| 108 | + <link rel="stylesheet" href="./assets/library/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous" /> |
| 109 | + </head> |
| 110 | + |
| 111 | + <body> |
| 112 | + |
| 113 | + <app-root></app-root> |
| 114 | + |
| 115 | + </body> |
| 116 | + |
| 117 | +</html> |
| 118 | +``` |
0 commit comments