My profile.html page data:
<ion-navbar>
<ion-title>Profile</ion-title>
</ion-navbar>
<input type="hidden" [(ngModel)]="person.id" />
<ion-item>
<ion-label floating>First Name</ion-label>
<ion-input [(ngModel)]= "person.firstname"></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Last Name</ion-label>
<ion-input [(ngModel)]= "person.lastname"></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Contact Number</ion-label>
<ion-input [(ngModel)]= "person.contactnumber"></ion-input>
</ion-item>
<button ion-button (click)="createProfile(person.id,person.firstname,person.lastname,person.contactnumber)">
<span *ngIf="person.id">Update Contact</span><span *ngIf="!person.id">Add Contact</span>
</button>
and my profile.ts page data:
import { Component } from ‘@angular/core’;
import { IonicPage, NavController, NavParams } from ‘ionic-angular’;
import { AngularFireAuth } from ‘angularfire4/auth’;
import { AngularFireDatabase,FirebaseListObservable } from ‘angularfire4/database’;
import * as firebase from ‘firebase/app’;
import { HomePage } from ‘…/home/home’;
import { LoggedinPage } from ‘…/loggedin/loggedin’;
@IonicPage()
@Component({
selector: ‘page-profile’,
templateUrl: ‘profile.html’,
})
export class ProfilePage {
persons: FirebaseListObservable<any>;
person = {
id : '',
firstname: '',
lastname: '',
contactnumber: ''
}
constructor(
public afDatabase: AngularFireDatabase,
public afAuth: AngularFireAuth,
public navCtrl: NavController,
public navParams: NavParams) {
this.persons = afDatabase.list('/profile');
this.person.id = this.navParams.get('key');
this.person.firstname = this.navParams.get('firstname');
this.person.lastname = this.navParams.get('lastname');
this.person.contactnumber = this.navParams.get('contactnumber');
console.log('keys is:',this.person.id);
console.log('firstname is:',this.person.firstname);
}
ionViewDidLoad() {
console.log('ionViewDidLoad ProfilePage');
}
createProfile(id, firstname, lastname, contactnumber) {
if(id) {
alert('update');
this.persons.update(id, {
firstname: firstname,
lastname: lastname,
contactnumber: contactnumber
}).then( newContact => {
this.navCtrl.pop();
}, error => {
console.log(error);
});
} else {
alert('create');
this.persons.push({
firstname: firstname,
lastname: lastname,
contactnumber: contactnumber
}).then( newContact => {
this.navCtrl.pop();
}, error => {
console.log(error);
});
}
}
}
Issue: its not updating the existing data, its create new again and again also in my profile.html page
if id exists it should display update contact instead of that it displays Add contact again and again, even when new user is added.