Ionic 3: ion- auto-complete - How to pass different data other than autocomplete generated data - ionic-framework

I have created an auto complete functionality in ionic 3 framework.
app.component.html
<form [formGroup]="myForm" (ngSubmit)="openModal()">
<div class="ion-form-group">
<ion-auto-complete [dataProvider]="ionAutoCompleteService"
[options]="{ placeholder : 'Search an Institution' }" formControlName="institution"
required>
</ion-auto-complete>
</div>
<button [disabled]="!myForm.valid" icon-left ion-button type="submit" block>
<ion-icon name="link"></ion-icon>
Link Institution
</button>
</form>
for this I also create the service provider as follow
import {AutoCompleteService} from 'ionic2-auto-complete';
import { Http } from '#angular/http';
import {Injectable} from "#angular/core";
import 'rxjs/add/operator/map'
#Injectable()
export class IonicAutoCompleteService implements AutoCompleteService {
labelAttribute = "name";
constructor(private http:Http) {
}
getResults(keyword:string) {
return this.http.get("https://aqueous-oasis-93790.herokuapp.com/institutions/search?name="+keyword)
.map(
result =>
{
console.log(result)
return result.json().institutions
.filter(item => item.name.toLowerCase().startsWith(keyword.toLowerCase()) )
});
}
}
this service provides me the name of institutions which are retrieve from the json file.
To access this service I just need to import the service in app.component.ts file
app.component.ts
import { Component } from '#angular/core';
import { IonicPage, NavController, NavParams, ModalController } from 'ionic-angular';
import { FormGroup, FormControl, Validators } from '#angular/forms';
import { IonicAutoCompleteService } from '../../../services/ionic-autocomplete.service';
import { AddAccountModalPage } from './add-account-modal/add-account-modal';
#IonicPage()
#Component({
selector: 'page-add-account',
templateUrl: 'add-account.html',
})
export class AddAccountPage {
constructor(public navCtrl: NavController, public navParams: NavParams,
public ionAutoCompleteService:IonicAutoCompleteService,
public modalCtrl:ModalController) {
}
ngOnInit(): void { }
myForm = new FormGroup({
institution: new FormControl('', [Validators.required])
})
submit(): void {
console.log(this.myForm.value.institution)
}
onClick() {
console.log("img clicked")
}
openModal() {
let modal = this.modalCtrl.create(AddAccountModalPage);
modal.present();
}
}
My question is that, When I am submit the form, the value in search box will be submitted which is name of institution.
I want to send the institution name to following ionic modal
I also want to send institution id as a modal parameter like (ngSubmit)="openmodal(institution_id)" in app.component.html file
modal.html
<ion-header>
<ion-toolbar>
<ion-title>
Add Account Modal
</ion-title>
<ion-buttons start>
<button ion-button (click)="dismiss()">
<span ion-text color="primary" showWhen="ios">Cancel</span>
<ion-icon name="md-close" showWhen="android,windows"></ion-icon>
</button>
</ion-buttons>
</ion-toolbar>
</ion-header>
modal.ts
import { Component } from '#angular/core';
import { IonicPage, NavController, NavParams, ViewController } from 'ionic-angular';
#IonicPage()
#Component({
selector: 'page-add-account-modal',
templateUrl: 'add-account-modal.html',
})
export class AddAccountModalPage {
institution = "india"
constructor(public navCtrl: NavController, public navParams: NavParams,
public viewCtrl: ViewController) {
}
ionViewDidLoad() {
}
dismiss() {
this.viewCtrl.dismiss();
}
}

You can send parameters like this:
openModal() {
let Modal = this.modalCtrl.create(AddAccountModalPage,
{ institution: this.formGroup.get('institution').value
});
Modal.present();
}
and after get this parameter in modal.ts:
institution: any;
constructor(public navCtrl: NavController,
public navParams: NavParams,
public viewCtrl: ViewController) {
}
ngOnInit() {
this.institution= this.navParams.get('institution');
}

Related

Problem with Camera Preview Plugin (ionic 3): Object(...) is not a function at CameraPreview.startCamera

I'm developing an ionic 3 application in which I need to show the camera interface inside the app screen and camera-preview seems to be the right and only solution to go with at this moment. However, when I trigger the startCamera function, I always get the error ' Object(...) is not a function at CameraPreview.startCamera'. Any help would be much appreciated.
Here are the steps I used for the installation:
From CLI:
ionic cordova plugin add https://github.com/cordova-plugin-camera-preview/cordova-plugin-camera-preview.git
npm install #ionic-native/camera-preview
2.Add to the provider list in module.ts file
import { CameraPreview } from '#ionic-native/camera-preview/ngx';
.....
providers: [
CameraPreview, ...
]
Below is the page where I would use the plugin:
import { Component, NgZone } from '#angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { CameraPreview, CameraPreviewPictureOptions, CameraPreviewOptions, CameraPreviewDimensions } from '#ionic-native/camera-preview/ngx';
#Component({
selector: 'page-submitdata2',
templateUrl: 'submitdata2.html',
})
export class Submitdata2Page {
public getWidth: number;
public getHeight: number;
public calcWidth: number;
cameraPreviewOpts: CameraPreviewOptions =
{
x: 40,
y: 100,
width: 220,
height: 220,
toBack: false,
previewDrag: true,
tapPhoto: true,
camera: this.cameraPreview.CAMERA_DIRECTION.BACK
}
constructor(
public cameraPreview: CameraPreview, private zone: NgZone,
public navCtrl: NavController, public navParams: NavParams) {
this.zone.run(() => {
this.getWidth = window.innerWidth;
this.getHeight = window.innerHeight;
});
console.log('width', this.getWidth);
this.calcWidth = this.getWidth - 80;
console.log('calc width', this.calcWidth);
}
ionViewDidLoad() {
console.log('ionViewDidLoad Submitdata2Page');
}
startCamera() {
debugger
this.cameraPreview.startCamera(this.cameraPreviewOpts).then(
(res) => {
console.log(res)
},
(err) => {
console.log(err)
});
}
stopCamera() {
this.cameraPreview.stopCamera();
}
takePicture() {
this.cameraPreview.takePicture()
.then((imgData) => {
(<HTMLInputElement>document.getElementById('previewPicture')).src = 'data:image/jpeg;base64,' + imgData;
});
}
SwitchCamera() {
this.cameraPreview.switchCamera();
}
showCamera() {
this.cameraPreview.show();
}
hideCamera() {
this.cameraPreview.hide();
}
}
The HTML:
<ion-header>
<ion-navbar>
<ion-title>Preview Page</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<h5>This is camera Preview Application..</h5>
<div class="displaybottom">
<button ion-button (click)="startCamera()"> Start Camera</button>
<button ion-button (click)="stopCamera()"> Stop Camera</button>
<button ion-button (click)="takePicture()"> Take Picture Camera</button>
<button ion-button (click)="SwitchCamera()"> Switch Camera</button>
<button ion-button (click)="showCamera()"> Show Camera</button>
<button ion-button (click)="hideCamera()"> Hide Camera</button>
</div>
<div class="pictures">
<p><img id="previewPicture" width="200" /></p>
<!--<p><img id="originalPicture" width="200"/></p>-->
</div>
</ion-content>
My Development Enviroment:

Ionic Framework 3 Error: Window Redirect Problems

I'm new in Ionic Framework, and I'm using version 3 to build a simple application that redirects the user to an .html page after logging in. I've edited the home.ts file to make the log in possible, and I've built a test .html called index.html, in the folder "pages/home" inside the ionic project, but I'm getting an error when I try to conned the "submit" button to this index.html page:
Code in home.html
<ion-header>
<ion-navbar>
<ion-title>
Login
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-list no-lines>
<ion-item>
<ion-label floating>User</ion-label>
<ion-input type="text" [(ngModel)]="username"></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Senha</ion-label>
<ion-input type="text" [(ngModel)]="password"></ion-input>
</ion-item>
<button block ion-button block (click)="signIn()">Sign in</button>
</ion-list>
</ion-content>
Code in home.ts
import { Component } from '#angular/core';
import { NavController, AlertController, IonicPage } from 'ionic-angular';
import { index } from 'c:/Ionic/task-1/src/pages/home/index.html';
#Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
public username : any = '';
public password : any = '';
constructor(public navCtrl: NavController, public alertCtrl: AlertController)
{
}
signIn() {
this.navCtrl.push('index.html');
}
}
The code in index.html
<ion-header>
<ion-navbar>
<ion-title>
Login
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<p>Testing HTML page redirect</p>
</ion-list>
</ion-content>
The error: https://i.stack.imgur.com/vRivh.png
What could I do?
In ionic 3 you can navigate through pages using NavController.You should create a component that you want to route.
For that you have to import the component(page)
HTML file
<button block ion-button block (click)="signIn()">Sign in</button>
TS file
import { NavController } from 'ionic-angular';
import { StartPage } from './start-page';
#Component(
selector: 'app-home',
templateUrl: 'home.html'
})
class HomePage {
constructor(public navCtrl: NavController){}
signIn() {
this.navCtrl.push(StartPage);
}
}
Refer NavController documentation in ionic
check here
In home.ts file :
import { Component } from '#angular/core';
import { NavController, AlertController, IonicPage } from 'ionic-angular';
import { IndexPage} from '../index/index';
#Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
public username : any = '';
public password : any = '';
constructor(public navCtrl: NavController, public alertCtrl: AlertController){}
signIn() {
this.navCtrl.push(IndexPage);
}
}

Ionic Http request re-occurring multiple times

I'm learning Ionic and stuck on an issue. My app is stuck in a loop in which it is repeatedly fetching information from a remote server and causing the browser and crash.
I setup a service, code below, to retrieve the data:
import { HttpClient } from '#angular/common/http';
import { Injectable } from '#angular/core';
/*
Generated class for the CategoriesServiceProvider provider.
See https://angular.io/guide/dependency-injection for more info on providers
and Angular DI.
*/
#Injectable()
export class CategoriesServiceProvider {
apiURL = 'https://reqres.in/api/users';
constructor(public http: HttpClient) {
console.log('Hello CategoriesServiceProvider Provider');
}
getUsers(){
return new Promise(resolve => {
this.http.get(this.apiURL).subscribe(data => {
resolve(data);
// console.log(data);
}, err => {
console.error(err);
});
});
}
}
Below is the code in my categories.ts file which uses the service above:
import { Component } from '#angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { CategoriesServiceProvider } from '../../providers/categories-service/categories-service';
/**
* Generated class for the CategoriesPage page.
*
* See https://ionicframework.com/docs/components/#navigation for more info on
* Ionic pages and navigation.
*/
#IonicPage()
#Component({
selector: 'page-categories',
templateUrl: 'categories.html',
providers: [CategoriesServiceProvider]
})
export class CategoriesPage {
constructor(public navCtrl: NavController, public navParams: NavParams, private categories: CategoriesServiceProvider) {
}
users: any;
getUsers(){
this.categories.getUsers().then(data => {
this.users = data;
console.log(this.users);
});
}
ionViewDidLoad() {
console.log('ionViewDidLoad CategoriesPage');
this.getUsers();
}
}
... followed by categories.html:
<ion-header>
<ion-navbar>
<ion-title>categories</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-list>
<ion-item *ngFor="let user of getUsers()">
TESTING - {{user.gender}}
</ion-item>
</ion-list>
</ion-content>
I don't know why the http request is repeating instead of fetching once and then going away.
here you are looping on the function to get your data:
<ion-content padding>
<ion-list>
<ion-item *ngFor="let user of getUsers()">
TESTING - {{user.gender}}
</ion-item>
</ion-list>
</ion-content>
Replace it with the retrieved data instead!
Something like :
<ion-content padding>
<ion-list>
<ion-item *ngFor="let user of this.users">
TESTING - {{user.gender}}
</ion-item>
</ion-list>
</ion-content>

Issue when retrieving data from Firebase using ionic+angularfire2

I am following a tutorial to create a Chat application using Ionic and AngularFire2.
However, I am having this error when I am trying to retrieve data from Firebase Database.
TypeError: Object(...) is not a function
at SwitchMapSubscriber.project (http://localhost:8100/build/vendor.js:74005:76)
at SwitchMapSubscriber._next (http://localhost:8100/build/vendor.js:62295:27)
at SwitchMapSubscriber.Subscriber.next (http://localhost:8100/build/vendor.js:20750:18)
at RefCountSubscriber.Subscriber._next (http://localhost:8100/build/vendor.js:20786:26)
at RefCountSubscriber.Subscriber.next (http://localhost:8100/build/vendor.js:20750:18)
at Subject.next (http://localhost:8100/build/vendor.js:23237:25)
at ConnectableSubscriber.Subscriber._next (http://localhost:8100/build/vendor.js:20786:26)
at ConnectableSubscriber.Subscriber.next (http://localhost:8100/build/vendor.js:20750:18)
at Notification.observe (http://localhost:8100/build/vendor.js:51893:50)
at AsyncAction.DelaySubscriber.dispatch (http://localhost:8100/build/vendor.js:76316:40)
My Chat.TS code
import { Component } from '#angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { AngularFireDatabase} from 'angularfire2/database';
import { Observable } from 'rxjs/Observable';
/**
* Generated class for the ChatPage page.
*
* See https://ionicframework.com/docs/components/#navigation for more info on
* Ionic pages and navigation.
*/
#IonicPage()
#Component({
selector: 'page-chat',
templateUrl: 'chat.html',
})
export class ChatPage {
username: string = '';
message : string = '';
//messages : object[] = [];
items: Observable<any[]>;
constructor(public db: AngularFireDatabase , public navCtrl: NavController, public navParams: NavParams) {
console.log(this.navParams);
this.username = this.navParams.get('username');
this.items = db.list('chat').valueChanges();
}
ionViewDidLoad() {
console.log('ionViewDidLoad ChatPage');
}
sendMessage(){
this.db.list('/chat').push({
username:this.username,
message:this.message
});
}
}
My Chat.HTML code
<ion-header>
<ion-navbar>
<ion-title>Chat</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ul>
<li *ngFor="let item of items | async">
{{ item | json }}
</li>
</ul>
</ion-content>
<ion-footer>
<ion-toolbar>
<ion-input type="text" [(ngModel)]="message"></ion-input>
<button ion-button icon-only (click)="sendMessage()"><ion-icon name="send"></ion-icon>
</button>
</ion-toolbar>
</ion-footer>
I had tried many different ways of doing it and I still get the same object error.
Anyone have any idea what is causing this error?
Thanks for your help.

Ionic Pages - changing content

app.html
<ion-header>
<ion-navbar>
<ion-title>Blabla</ion-title>
<button ion-button color="primary" (click)="home()">Home</button>
<button ion-button color="primary" (click)="second()">Second</button>
</ion-navbar>
</ion-header>
<ion-content>
</ion-content>
<ion-nav #myNav [root]="rootPage"></ion-nav>
app.component.ts
import { Component, ViewChild } from '#angular/core';
import { Platform, NavController } from 'ionic-angular';
import { StatusBar } from '#ionic-native/status-bar';
import { SplashScreen } from '#ionic-native/splash-screen';
import { HomePage } from '../pages/home/home';
import { SecondpagePage } from '../pages/secondpage/secondpage';
#Component({
templateUrl: 'app.html'
})
export class MyApp {
#ViewChild('myNav') nav: NavController;
rootPage:any = HomePage;
constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
statusBar.styleDefault();
splashScreen.hide();
});
}
home() {
this.nav.setRoot(HomePage);
}
second() {
this.nav.setRoot(SecondpagePage);
}
}
In HomePage and SecondPage I put only <ion-content>Test1</ion-content> and <ion-content>Test2</ion-content>.
Now I have that situation - the header "covers" the rest. So I don't see any text because it's below the header. How can I change that behaviour?
And also I want to use one static immovable header/navbar (because there would be an animation). So content which can be changed is under that header/navbar.
Try moving the ion-nav into the ion-content, that should make it behave correctly.