Here the video i am following this video, I did same as video, but still showing me error undefined value. anyone had idea what to do. Please help me and i am using ionic 3,
Here the video i am following this video, I did same as video, but still showing me error undefined value. anyone had idea what to do. Please help me and i am using ionic 3,
home.html
<ion-header>
<ion-navbar>
<ion-title>
Ionic Blank
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-list>
<ion-item>
<ion-label floating>Username</ion-label>
<ion-input type="text" name="username" ></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Password</ion-label>
<ion-input type="password" name="password"></ion-input>
</ion-item>
</ion-list>
<div padding>
<button ion-button color="primary" (click)="signIn()" block>Sign In</button>
</div>
</ion-content>
home.ts
import { Component, ViewChild } from '#angular/core';
import { NavController, AlertController } from 'ionic-angular';
#Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
#ViewChild('username') uname;
#ViewChild('passwoed') password;
constructor(public navCtrl: NavController, public alertCtrl:
AlertController ) {
}
signIn() {
console.log(this.uname.value, this.password.value)
if(this.uname.value == "admin" && this.password.value == "admin") {
let alert = this.alertCtrl.create({
title: 'Login Successfull!',
subTitle: 'You are logged in!',
buttons: ['OK']
});
alert.present();
}
}
}
You got mistake on home.html, i resolve this.
<ion-header>
<ion-navbar>
<ion-title>
Ionic Blank
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-list>
<ion-item>
<ion-label floating>Username</ion-label>
<ion-input type="text" #username ></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Password</ion-label>
<ion-input type="password" #password></ion-input>
</ion-item>
</ion-list>
<div padding>
<button ion-button color="primary" (click)="signIn()" block>Sign In</button>
</div>
</ion-content>
Related
Iam newbie to ionic and creating a basic mobile app after learning ionic online.
I had created a customer details form page where customers need to enter their details like, name, gender, dob, city etc.
Instead of manually entering the entering the details, I want to put QR Scanner in the page so that when Aadhaar or any other barcode details saver QR code gets scanned the customer details needs to gets filled automatically.
Below is the code
<ion-content>
<ion-item>
<ion-label position="floating" >Full Name</ion-label>
<ion-input type="text" ></ion-input>
</ion-item>
<ion-item>
<ion-label position="floating" >DOB</ion-label>
<ion-input type="text" ></ion-input>
</ion-item>
<ion-item>
<ion-label position="floating" >Gender</ion-label>
<ion-input type="text" ></ion-input>
</ion-item>
<ion-item>
<ion-label position="floating" >Address</ion-label>
<ion-input type="text" ></ion-input>
</ion-item>
<ion-item>
<ion-label position="floating" >State</ion-label>
<ion-input type="text" ></ion-input>
</ion-item>
<ion-item>
<ion-label position="floating" >District</ion-label>
<ion-input type="text" ></ion-input>
</ion-item>
<ion-item>
<ion-label position="floating" >City</ion-label>
<ion-input type="text" ></ion-input>
</ion-item>
<ion-item>
<ion-label position="floating" >Postal Code</ion-label>
<ion-input type="text" ></ion-input>
</ion-item>
I tried installing the ionic qr scanner plugins
ionic cordova plugin add cordova-plugin-qrscanner
npm install #ionic-native/qr-scanner
But no idea how to proceed further. Please help me on this, i got struct on this from last 10 days
Thank you
Have a look at my code for scanning QR codes (Ionic 4)
To be honest, I like the barcode-scanner way more than the qr code scanner and basically the outcome is the same.
https://ionicframework.com/docs/native/barcode-scanner
import { Component } from '#angular/core';
import { Router, NavigationEnd } from '#angular/router';
import { Platform, AlertController } from '#ionic/angular';
import { DatabaseService} from '../../providers/database/database.service';
import { BarcodeScanner } from '#ionic-native/barcode-scanner/ngx';
#Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
current:any;
constructor(
private platform: Platform,
private router: Router,
public db: DatabaseService,
public barcodeScanner: BarcodeScanner,
private alertController: AlertController
)
{}
scan()
{
this.barcodeScanner.scan().then(barcodeData => {
if(barcodeData.cancelled)
{
return
}
this.db.checkQRCode(barcodeData.text)
.then(doc => {
})
}).catch(err => {
console.log('Error', err);
});
}
}
I'm new to ionic and I want to know how to add an <ion-menu> for just one component (the first tab of my tabs section). how can I do this?
I need add this:
<ion-menu [content]="mycontent">
<ion-content>
<button ion-button (click)="show_message()">
</buttont>
</ion-content>
this is my code:
https://stackblitz.com/edit/ionic-5bunxz?file=pages/contact/contact.ts
Modify your tab 1 page (AboutPage in your case) like below
<ion-menu [content]="content">
<ion-header no-shadow no-border>
<ion-item no-lines>
<!-- Phase 2/3 PLEASE DO NOT DELETE THIS -->
<ion-avatar menuClose item-start>
<!-- <ion-avatar menuClose item-start> -->
<img [src]="'assets/imgs/user.png'">
</ion-avatar>
<h2>Test Emp</h2>
<h3>test3333</h3>
<p>Test4564565</p>
</ion-item>
</ion-header>
<ion-content style="background: #fff">
<ion-list no-lines>
<ion-item class="drawer-item" menuClose >
<ion-icon name="home" item-start></ion-icon>
Test
</ion-item>
<ion-item class="drawer-item" menuClose >
<ion-icon name="briefcase" item-start></ion-icon>
Test2
</ion-item>
</ion-list>
</ion-content>
</ion-menu>
<ion-header no-shadow no-border>
<ion-navbar>
<button ion-button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
<ion-title text-uppercase>About</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding #content>
esta es la segunda XDDD
<button ion-button block color="primary" navPop>back</button>
</ion-content>
add your menu component code in the app component page. So, your app.component.html page will look like below.
<ion-menu [content]="content">
<ion-header>
<ion-toolbar>
<ion-title>Welcome Home</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-list>
<button menuClose ion-item *ngFor="let p of pages"
(click)="openPage(p)">
{{p.title}}
</button>
</ion-list>
</ion-content>
</ion-menu>
<ion-nav [root]="rootPage" #content swipeBackEnabled="false"></ion-nav>
You can add dynamic pages from the component file as shown below.
Here is my app.component.ts file.
export class MyApp {
pages: Array<{title: string, component: any}>;
constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {
this.pages = [{title: 'Home', component: HomePage},
{title: 'About', component: AboutPage}];
}
}
So, now this menu bar will be shown on each page of your app. Now, suppose you want to hide this page in About page then you can write below code in the component of your about page.
export class AboutPage {
constructor(public navCtrl: NavController, public navParams: NavParams,
public menu: MenuController) {}
ionViewDidEnter() {
//to disable menu, or
this.menu.enable(false);
}
}
So, by following above method you can hide menu button in any of your pages.
Im using ionic 3 for my mobile application, I have some issues with my input fields which do not move automatically to the next field. For example when I click the first input filed and fill the first one, the cursor does not move to the next field. How to do that correctly?
<ion-grid>
<ion-row>
<ion-col>
<ion-item >
<ion-input type="tel" placeholder="*" maxlength="1" tabindex="1" ></ion-input>
</ion-item>
</ion-col>
<ion-col >
<ion-item>
<ion-input type="tel" placeholder="*" maxlength="1" tabindex="2" ></ion-input>
</ion-item>
</ion-col>
<ion-col >
<ion-item>
<ion-input type="tel" placeholder="*" maxlength="1" tabindex="3" ></ion-input>
</ion-item>
</ion-col>
<ion-col >
<ion-item>
<ion-input type="tel" placeholder="*" maxlength="1" tabindex="4" ></ion-input>
</ion-item>
</ion-col>
</ion-row>
</ion-grid>
You can use the following approach, there could be better approaches i'm just sharing what i know.
What i am doing here is setting a reference of the next element (eg: #b), and on keyup event i am passing that reference to my function in .ts file which only calls the .setFocus() on the referenced element.
<ion-grid>
<ion-row>
<ion-col>
<ion-item >
<ion-input type="tel" placeholder="*" maxlength="1" tabindex="1" (keyup)="moveFocus(b)" ></ion-input>
</ion-item>
</ion-col>
<ion-col >
<ion-item>
<ion-input type="tel" placeholder="*" maxlength="1" tabindex="2" #b (keyup)="moveFocus(c)" ></ion-input>
</ion-item>
</ion-col>
<ion-col >
<ion-item>
<ion-input type="tel" placeholder="*" maxlength="1" tabindex="3" #c (keyup)="moveFocus(d)" ></ion-input>
</ion-item>
</ion-col>
<ion-col >
<ion-item>
<ion-input type="tel" placeholder="*" maxlength="1" tabindex="4" #d ></ion-input>
</ion-item>
</ion-col>
</ion-row>
</ion-grid>
.ts:
import { Component } from '#angular/core';
#Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor() {
}
moveFocus(nextElement) {
nextElement.focus();
}
}
You can try something with the (keyup) or (keydown) events. There is also documentation available on the angular docs that talks about forms and user Input.
HTML
<input (keyup)="onKey($event)">
.ts
onKey(event) {
if (event.key === "Enter") {
console.log(event);
}
}
This can be achieved by using the nextElementSibling , you can use the Keyup event to achieve this , here is the Pseudo code
Add keyup event to the event fields as follows (keyup)="keytab($event)"
keytab(event){
let element = event.srcElement.nextElementSibling; // get the sibling element
if(element == null) // check if its null
return;
else
element.focus(); // focus if not null
}
In order to make it more clear manner you can create a directive also as follows
tabindex.directive.ts:
import { Directive, HostListener, Input } from '#angular/core';
import { TextInput } from 'ionic-angular';
#Directive({
selector: '[yourTabindex]'
})
export class TabindexDirective {
constructor(private inputRef: TextInput) { }
#HostListener('keydown', ['$event']) onInputChange(e) {
var code = e.keyCode || e.which;
if (code === 13) {
e.preventDefault();
this.inputRef.focusNext();
}
}
}
Don't forget to include this directive in Modules page and you can use it on your input fields as follows ,as a sample:
<ion-input type="tel" placeholder="*" maxlength="1" tabindex="1" yourTabindex ></ion-input>
Adding "scrollAssist: true" and "autoFocusAssist: true" to app.module in the #ngModule would solve this problem.
imports: [
IonicModule.forRoot(MyApp, {
scrollAssist: true,
autoFocusAssist: true
})
],
I'm working on a project in Ionic and need to use reactive forms from Angular 5. I need to allow a user to select a language preference and I'm doing that with radio-group and ion-radio, but when I make changes to the form, I'm getting
ERROR Error: There is no FormControl instance attached to form control element with name: 'preferredLanguage'
Here is my template with the form
<ion-header>
<ion-navbar>
<ion-title>User Profile</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<form *ngIf="user" [formGroup]="profileForm" (ngSubmit)="saveUser()">
<ion-item>
<ion-label>Display Name</ion-label>
<ion-input type="text" formControlName="displayName"></ion-input>
</ion-item>
<ion-item>
<ion-label>First Name</ion-label>
<ion-input type="text" formControlName="firstName"></ion-input>
</ion-item>
<ion-item>
<ion-label>Last Name</ion-label>
<ion-input type="text" formControlName="lastName"></ion-input>
</ion-item>
<ion-item>
<ion-label>Email</ion-label>
<ion-input type="text" formControlName="email"></ion-input>
</ion-item>
<ion-item>
<ion-label>Street</ion-label>
<ion-input type="text" formControlName="street"></ion-input>
</ion-item>
<ion-item>
<ion-label>City</ion-label>
<ion-input type="text" formControlName="city"></ion-input>
</ion-item>
<ion-item>
<ion-label>State</ion-label>
<ion-input type="text" formControlName="state"></ion-input>
</ion-item>
<ion-item>
<ion-label>Zip Code</ion-label>
<ion-input type="text" formControlName="zip"></ion-input>
</ion-item>
<ion-list radio-group formControlName="preferredLanguage">
<ion-list-header>
Select Preferred Language
</ion-list-header>
<ion-item>
<ion-label>English</ion-label>
<ion-radio value="en-US"></ion-radio>
</ion-item>
<ion-item>
<ion-label>Spanish</ion-label>
<ion-radio value="es-US"></ion-radio>
</ion-item>
</ion-list>
<button type="submit" ion-button>Submit Profile</button>
</form>
</ion-content>
Here is my method that initializes the form
initProfileForm() {
this.profileForm = this.formBuilder.group({
displayName: [this.user.profile.displayName, Validators.required],
firstName: [this.user.profile.firstName, Validators.required],
lastName: [this.user.profile.lastName, Validators.required],
email: [this.user.profile.email, Validators.required],
street: [this.user.profile.street, Validators.required],
city: [this.user.profile.city, Validators.required],
state: [this.user.profile.state, Validators.required],
zip: [this.user.profile.zip, Validators.required] ,
preferredLanguage: [this.user.profile.preferredLanguage,
Validators.required]
});
}
Has anyone else had this issue and how did you solve it?
I ended up coming up with a solution. This blog helped me come up with an answer: https://robferguson.org/blog/2017/11/19/ionic-3-and-forms/ Apparently, radio-group doesn't work with formControlName so instead I ended up using this:
<ion-list radio-group [formControl]="profileForm.controls['preferredLanguage']">
<ion-list-header>
Select Preferred Language
</ion-list-header>
<ion-item>
<ion-label>English</ion-label>
<ion-radio value="en-US"></ion-radio>
</ion-item>
<ion-item>
<ion-label>Spanish</ion-label>
<ion-radio value="es-US"></ion-radio>
</ion-item>
</ion-list>
This fixed the issue so there is no longer an error.
Make some amendments in your HTML code:
<ion-item>
<ion-label>English</ion-label>
<ion-radio value="en-US" formControlName="preferredLanguage"></ion-radio>
</ion-item>
<ion-item>
<ion-label>Spanish</ion-label>
<ion-radio value="es-US" formControlName="preferredLanguage"></ion-radio>
</ion-item>
So basically instead of using it on parent use it individually for each radio button. This worked for me.
Hope it helps!
get preferredLanguage() {
return this.profileForm.get('preferredLanguage').value;
}
set preferredLanguage(ev: CustomEvent) {
this.profileForm.get('preferredLanguage').patchValue(ev.detail.value);
}
<ion-radio-group [value]="preferredLanguage" (ionChange)="preferredLanguage = $event">
<ion-item>
<ion-label>English</ion-label>
<ion-radio value="en-US"></ion-radio>
</ion-item>
<ion-item>
<ion-label>Spanish</ion-label>
<ion-radio value="es-US"></ion-radio>
</ion-item>
</ion-radio-group>
I am developing a SIgnup page in ionic 3 and the no of fields are 10.
SO I am using a p tag to show validation and when the user hits submit I want to take him all the way up to the start of the page which has my header and all.How can I achieve this?I have tried everything.
Below is my code:
<ion-content padding class="label-cl green-bg acct-sct item-row" style="position:fixed">
<h2 class="center-col">Create An Account</h2>
<form #f="ngForm" (ngSubmit)="signUp(f)">
<ion-item>
<ion-label floating>Username</ion-label>
<ion-input type="text" name="name" ngModel required></ion-input>
</ion-item>
<p style="color:red;text-align: left" *ngIf="usernameError">Invalid Username</p>
<ion-item>
<ion-label floating>Email</ion-label>
<ion-input type="email" name="email" ngModel required></ion-input>
</ion-item>
<p style="color:red;text-align: left" *ngIf="emailError">Invalid E-mail</p>
<ion-item>
<ion-label floating>Password</ion-label>
<ion-input type="Password" name="password" ngModel required></ion-input>
</ion-item>
<p style="color:red;text-align: left" *ngIf="passwordLengthError">Invalid Password</p>
<ion-item>
<ion-label floating>Confirm Password</ion-label>
<ion-input type="Password" name="confirm_password" ngModel required></ion-input>
</ion-item>
<div class="button-inline">
<button ion-button class="yellow-cl" [disabled]="!f.valid">Submit</button>
</div>
</form>
</ion-content>
It has more fields but I am showing only these just for the sake of clarity.
Thanks in advance.
Add the below code to your .ts file
import { Component, ViewChild } from '#angular/core';
import { Content } from 'ionic-angular';
#Component({...})
export class SignUpPage{
#ViewChild(Content) content: Content;
signUp() {
this.content.scrollToTop();
}
}
We used Angular's #ViewChild annotation to get a reference to the ionic content component which has the method scrollToTop().
Find more details in the Official docs