Saturday 10 December 2022

Ionic Angular #1 Simple Demo

IONIC ANGULAR TUTORIAL

Step 1 : Install Ionic

npm install -g ionic

Step 2:

Create an Ionic angular  App by

npm install -g @ionic/cli native-run cordova-res

Step 3:

create an ionic app with tabs

ionic start demo-ionic tabs --type=angular --capacitor

Step 4 

cd demo-ionic

npm install @capacitor/camera @capacitor/preferences @capacitor/filesystem

Step 5

npm install @ionic/pwa-elements

Step 6 : edit in VS studio

Next, import @ionic/pwa-elements by editing src/main.ts.

import { defineCustomElements } from '@ionic/pwa-elements/loader';

defineCustomElements(window);

Step 6:

Run the App by 

ionic serve

Check out the output similar to this browser window



Step 7:

In the file  /src/app/tab2/tab2.page.htm

change both <ion-title> Tab2 to <ion-title> Demo Photo


Append the code  changing icon from ellipse to camera

<ion-content>

  <ion-fab vertical="bottom" horizontal="center" slot="fixed">

    <ion-fab-button>

      <ion-icon name="camera"></ion-icon>

    </ion-fab-button>

  </ion-fab>

</ion-content>

Step 8:

Modify src/app/tabs/tabs.page.html. file  

Change the label to <ion-label> Photos

check the changes made in running browser and clicking Photos icon


Let us learn further Later ....👀👀👀👀


Thursday 1 December 2022

Angular #8 Forms

Forms

2 - Types namely

1. Template driven

2. Reactive Forms


3 - Building Blocks namely 

FormControl (single Item), 

FormGroup (collection of Form Controls)

FormArray (Array of Form Controls)


Method 1 - TDF

ng new tdf --routing=true --style=css

ng serve -o to check

Open app.module.ts and add FormsModule to it 

import { FormsModule} from '@angular/forms';

add that in imports section

 imports: [
    BrowserModule,
    AppRoutingModule,
    FormsModule
  ],

add the following in AppComponent class in app.component.ts file

countryList:country[] = [
    new country("1", "India"),
    new country('2', 'USA'),
    new country('3', 'England')
  ];

 Add  the following class country:

export class country {
  id:string;
  name:string;
 
  constructor(id:string, name:string) {
    this.id=id;
    this.name=name;
  }
}

Delete the old contents in app.component.html and add the following contents 


form>
  <table border="2" >
  <tr>
 
    <label for="firstname">First Name :</label>
    <input type="text" id="firstname" name="firstname">
  </tr>
 
  <tr>
    <label for="lastname">Last Name:</label>
    <input type="text" id="lastname" name="lastname">
  </tr>
 
  <tr>
    <label for="email">Email :</label>
    <input type="text" id="email" name="email">
  </tr>
 
  <tr>
    <label for="gender">Gender :</label>
    <input type="radio" value="male" id="gender" name="gender"> Male
    <input type="radio" value="female" id="gender" name="gender"> Female
  </tr>
 
  <tr>
    <label for="isMarried">Married :</label>
    <input type="checkbox" id="isMarried" name="isMarried">
  </tr>
 
  <tr>
  <label for="country">country :</label>
  <select name="country" id="country">
    <option selected="" value=""></option>
    <option [ngValue]="c.id" *ngFor="let c of countryList">
      {{c.name}}
    </option>
  </select>
  </tr>
 
  <tr>
    <button type="submit">Submit</button>
  </tr>
</table>
</form>

change the <form> line as shown below

  <form #contactform="ngForm" (ngSubmit)="onSubmit(contactForm)"></form>

add ngModel to all six form elements: This will be similar to these contents

 <p>Template Driven Form</p>

<!-- <form> -->
<form #contactForm="ngForm" (ngSubmit) = "onSubmit(contactForm)">
<table border="2" >
  <tr>
 
    <label for="firstname">First Name :</label>
    <input type="text" id="firstname" name="firstname" ngModel>
  </tr>
 
  <tr>
    <label for="lastname">Last Name:</label>
    <input type="text" id="lastname" name="lastname" ngModel>
  </tr>
 
  <tr>
    <label for="email">Email :</label>
    <input type="text" id="email" name="email" ngModel>
  </tr>
 
  <tr>
    <label for="gender">Gender :</label>
    <input type="radio" value="male" id="gender" name="gender" ngModel> Male
    <input type="radio" value="female" id="gender" name="gender" ngModel> Female
  </tr>
 
  <tr>
    <label for="isMarried">Married :</label>
    <input type="checkbox" id="isMarried" name="isMarried" ngModel>
  </tr>
 
  <tr>
  <label for="country">country :</label>
  <select name="country" id="country" ngModel>
     <option [ngValue]="c.id" *ngFor="let c of countryList">
      {{c.name}}
    </option>
  </select>
  </tr>
 
  <tr>
    <button type="submit">Submit</button>
  </tr>
</table>
</form>

app.component.ts will have these contents

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {
  title = 'tdf';

  countryList:country[]=[
    new country("1", "india"),
    new country("2", "indonesia"),
    new country("3", "usa"),
    new country("4", "uk")
  ]
 
  onSubmit(contactForm: { value: any; }){
    console.log(contactForm.value);
  }
}

export class country {
  id: string;
  name: string;

  constructor(id:string,name:string){
    this.id=id;
    this.name=name;
  }
}

Now run by

ng serve -o and check the browser window output(similar to the following) with console output checking the entered values after submit button pressed.


Add the following contents in the app.component.ts

Entered Values:
<table>
  <tr>
    <td>
      Value : {{contactForm.value | json }}
    </td>
  </tr>
  <tr>
    <td>
      Touched : {{contactForm.touched  }}
    </td>
  </tr>
  <tr>
    <td>
      Submitted : {{contactForm.submitted  }}
    </td>
  </tr>
  </table>
 

Check the browser output similar to after you entered form and submitted:


Try this exercise to understand th values:

In app.component.html try commenting (by marking all and ctrl+/ ) the old contents add the following

 <form>
    <input type="text" name="firstname" #fname="ngModel" ngModel>
  </form>
<pre>
Value    : {{fname.value}} <br>
valid    : {{fname.valid}} <br>
invalid  : {{fname.invalid}} <br>
touched  : {{fname.touched}}
</pre>

Check the browser output 

before entering


after entering



In app.component..html add the following contents  commenting the old:
 

<form #contactForm="ngForm" (ngSubmit)="onSubmit(contactForm)"></form>
<div ngModelGroup="address">
  <p>
    <label for="city">City</label>
    <input type="text" name="city" ngModel>
  </p>
  <p>
    <label for="street">Street</label>
    <input type="text" name="street" ngModel>
  </p>
  <p>
    <label for="pincode">Pin Code</label>
    <input type="text" name="pincode" ngModel>
  </p>
</div>
</form>

Check the output values (similar to the one shown below) for address in browser :

I think you catch some basic points in form handling 



Method 2. Reactive Forms

Step 1:

Create a project reactive-demo by

ng new demo-react

Step 2:

Open app.module.ts and  add in the beginning 

import { ReactiveFormsModule } from '@angular/forms';  

and add the following in import section

imports: [
    ...  
    ReactiveFormsModule

Step 3:

Modify app.component.html, deleting the all the old  contents and add these following code:


 <h1> {{title}} </h1>
 <hr color="red">
 <form [formGroup] = "loginForm" (ngSubmit)="loginUser()">
    <label>Name : </label><input type="text" place holder="enter name" formControlName="user">
    <br><br>
    <label>Password : </label>
    <input type="password" place holder="enter password" formControlName="password"> <br>

    <button>Login</button>
 </form>


Please note 

1. Form with [formGroup] and (ngSubmit)="loginUser()" function

2. formControleName = user and formControleName=password attributes in <input> tag

Step 4:

Modify the app.component.ts by adding the following as shown below

import { Component } from '@angular/core';
import {FormGroup, FormControl} from '@angular/forms';


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'Angular Demo-React-Form';
  loginForm = new FormGroup({
    user: new FormControl(''),
    password: new FormControl(''),
  })
  loginUser(){
   console.log(this.loginForm.value)
  }
}

Step 5:

Now run by 

ng serve -o

Check console output after clicking login Button to see the o/p similar to the following:

before entering user name and password



After Entering and clicking the button Login. see in console, the o/p will be similar to this.

Happy Angular Forms....☺☺☺.!!!




Making Prompts for Profile Web Site

  Prompt: Can you create prompt to craft better draft in a given topic. Response: Sure! Could you please specify the topic for which you...