Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • How to Implement Login Using AnuglarJS in Ionic PhoneGap App in 4 Simple Steps

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 896
    Comment on it

    Ionic is a Phone Gap which is driven by AngularJs and has a common code for both Iphone and Andorid devices, it is free open-sources mobile SDK used for developing hybrid/native apps. Today in this tutorial, we will learn how to make a simple login in Ionic.



    So lets get started by following steps:-

    Step 1: We need a login page so will generate this page by terminal.
    Ionic provide a command which will generate a page automatically

    ionic generate page Login

    Now this page will be shown in src/pages/login


    Step 2: Now open this file login.html and its code will go like this:-

    <ion-content class="backgound">
        Login Now
        <form (ngSubmit)="logForm()">
          <ion-list no-line>
            <ion-input type = "text" placeholder ="username" #username ></ion-input>
            <ion-input type = "password" placeholder ="enter passod" #password></ion-input>
         <a>forget pass?</a>
         <button ion-button block outline color="light" (click)="login()" >Sign In</button>
    <button ion-button clear full color ="light" (click)="registration()">Dont have a account, Create one</button>


    Step 3: After using the HTML we have to use the JS code which will send the POST. This code is to be written in login.ts file. This file is also will be in same directory.
    Open this file and its code will be go like this:-

    import { Component, ViewChild } from '@angular/core';
    import { IonicPage, NavController, NavParams , AlertController} from 'ionic-angular';
    import { RegistrationPage } from '../registration/registration';
    import { HomePage } from "../home/home";
    import { Http, Headers } from "@angular/http";
    import { Storage } from '@ionic/storage';
    * Generated class for the LoginPage page.
    * See for more info
    * on Ionic pages and navigation.
     selector: 'page-login',
     templateUrl: 'login.html',
    export class LoginPage {
     @ViewChild('username') uname;
     @ViewChild('password') password;
     constructor(public navCtrl: NavController, public navParams: NavParams,public alertCtrl: AlertController, private http: Http, private storage: Storage) {
     ionViewDidLoad() {
      console.log('ionViewDidLoad LoginPage');
      let headers =new Headers();
      headers.append('Content-type', 'application/json');
      let body = {
       action: "login", username: this.uname.value, password: this.password.value
      };'http://yourhost/ionic/Apiv10Controller', JSON.stringify(body), {headers: headers})
      .map(res => res.json())
      .subscribe(data => {
        if(data.status == 'success'){
   'username', data.status);
             let alert = this.alertCtrl.create({
             title: 'Login success!',
             subTitle: 'Login is successfull proceed to home page',
             buttons: ['OK']
   'username', data.status);
             let alert = this.alertCtrl.create({
             title: data.status,
             subTitle: data.message,
             buttons: ['OK']
     //alert('get the registration');

    In the code above, you will see the login() function is sending the form data to server. Also we have have to use the Header, Http and AlertController in this app. So you have to add these modules as written in the code above.

    Step 4: This is the last step for redirecting user to home page once the login is correct.
    So we have use the page redirect to home in the login true case and its code is as below:-


    Similarly if login is failed then you will get  data.status = 'fail'. In this case you can redirect user to again login page:-



    The output of the code above is as in screen shots below:



    How to Implement Login Using AnuglarJS in Ionic PhoneGap App in 4 Simple Steps

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: