Hello friends, welcome back to my blog. Today in this blog post, I am going to tell you, Angular 11 Crud Tutorial with Nodejs + MySQL – Part 1.
Angular 11 came and if you are new then you must check below link:
Friends now I proceed onwards and here is the working code snippet for Angular 11 Crud Tutorial with Nodejs + MySQL – Part 1 and please use carefully this to avoid the mistakes:
1. Firstly friends we need fresh angular 11 setup and for this we need to run below commands but if you already have angular 11 setup then you can avoid below commands. Secondly we should also have latest node version installed on our system:
npm install -g @angular/cli ng new angularcrud //Create new Angular Project cd angularcrud // Go inside the Angular Project Folder ng serve --open // Run and Open the Angular Project http://localhost:4200/ // Working Angular Project Url
2. Now friends, here we need to run below commands into our project terminal to install bootstrap(for good looks)modules into our angular application:
npm install bootstrap --save
3. After done with commands add below code into you angular.json file:
"styles": [ ... "node_modules/bootstrap/dist/css/bootstrap.min.css" ]
4. Now friends, we need to run below commands to run our angular project:
ng serve --o
5. Now friends we just need to add below code into src/app/app.module.ts file:
... import {ReactiveFormsModule} from '@angular/forms'; ... imports: [ ... ReactiveFormsModule ]
6. Now friends we just need to add below code into src/app/app.component.ts file:
import { Component } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { constructor(private formBuilder: FormBuilder) {} //Add User Details From Functionality starts addForm: FormGroup; submitted = false; get f() { return this.addForm.controls; } onSubmit() { this.submitted = true; // stop here if form is invalid if (this.addForm.invalid) { return; } } ngOnInit() { //Add form validations this.addForm = this.formBuilder.group({ email: ['', [Validators.required, Validators.email]], firstname: ['', [Validators.required]] }); } }
7. Now friends we just need to add below code into src/app/app.component.html file to see the output on browser:
<div class="container mt-5 mb-5"> <hr /> <h1 class="text-center">Therichpost.com</h1> <hr /> <form [formGroup]="addForm" (ngSubmit)="onSubmit()"> <div class="row"> <div class="col-sm-6"> <div class="form-group"> <label>Username</label> <input type="text" formControlName="firstname" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.firstname.errors }" /> <div *ngIf="submitted && f.firstname.errors" class="invalid-feedback"> <div *ngIf="f.firstname.errors.required">Name is required</div> </div> </div> </div> <div class="col-sm-6"> <div class="form-group"> <label>Email</label> <input type="text" formControlName="email" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.email.errors }" /> <div *ngIf="submitted && f.email.errors" class="invalid-feedback"> <div *ngIf="f.email.errors.required">Email is required</div> <div *ngIf="f.email.errors.email">Email must be a valid email address</div> </div> </div> </div> </div> <button type="submit" class="btn btn-primary">Add</button> </form> </div>
Now we are done friends. If you have any kind of query or suggestion or any requirement then feel free to comment below.
In next post, I will tell you, How to save reactive form data into MySQL backend via NodeJS.
Note: Friends, I just tell the basic setup and things, you can change the code according to your requirements. For better understanding must watch video above.
I will appreciate that if you will tell your views for this post. Nothing matters if your views will be good or bad.
Jassa
Thanks
Recent Comments