Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Angular 14 Getters & Setters Two Way Data Binding Form Input Validation Tutorial For Beginners

Posted on February 8, 2023

 

 

Welcome folks today in this blog post we will be using the validating the input field data using two way data binding & getters and setters method using typescript in browser. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to make  a new angular project using the below command as shown below

 

 

ng new sampleproject

 

 

cd sampleproject

 

 

And after that you will see the below directory structure of the angular app as shown below

 

 

 

 

 

And now we need to go to app.component.html file and copy paste the below html code here we will have the input field where we will be assigning the ngModel directive as shown below

 

 

app.component.html

 

 

1
2
<p>The entered name is: {{name}}</p>
<input type="text" [(ngModel)]="validateName">

 

 

And now we need to define the validateName function inside the app.component.ts file as shown below

 

 

app.component.ts

 

 

TypeScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import {Component } from '@angular/core';
 
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent  {
  
  title = "sharing data"
 
  name:any
 
  get validateName():string{
    return this.name
  }
 
  set validateName(value:any){
    this.name = value
    if(this.name == "Kane"){
      alert("Name already taken")
    }
  }
 
}

 

 

 

As you can see we have the get and the set methods where we are returning the value of the name variable and then inside the set method we are passing the entered value and then we are checking if the value is equal to kane and then we are showing the alert statement as shown below

 

 

 

 

Recent Posts

  • Android Java Project to Crop,Scale & Rotate Images Selected From Gallery and Save it inside SD Card
  • Android Kotlin Project to Load Image From URL into ImageView Widget
  • Android Java Project to Make HTTP Call to JSONPlaceholder API and Display Data in RecyclerView Using GSON & Volley Library
  • Android Java Project to Download Youtube Video Thumbnail From URL & Save it inside SD Card
  • Android Java Project to Embed Google Maps & Add Markers Using Maps SDK
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme