I’m still fairly new to Ionic. I’m using a provider which is registered in the app.module as a provider for authentication as I’m going to need users id and type throughout the app. In the login page I can see the account.email etc, which are bound to the interface with [(ngModel)]. The provider is also injected in the constructor as a parameter. I.e. constructor(public accountSrv: AccountService) All good.
The provider has set and get methods as well as login & logout. But cannot access any of the data instantiated by the login page, so cannot do login. Console.log logs data as unidentified.
I’m using lazy loading of pages with a blank starter.
Login page is home.ts
import { Component } from '@angular/core';
import { IonicPage, NavController, ToastController } from 'ionic-angular';
import { Account } from './../../models/account.interface';
import { AccountService } from './../../providers/account.service';
@IonicPage()
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
account = {} as Account;
constructor(private toastCtrl: ToastController,
private navCtrl: NavController,
public accSrv: AccountService
) {
this.accSrv.setEmail(this.account.email);
this.accSrv.setPassword(this.account.password);
}
doLogin() {
// For some reason accSrv is not seeing the account info
let result = this.accSrv.login(); // *** result is also unidentified
}
}
account.service.ts
import { Account } from './../models/account.interface';
import { Injectable } from '@angular/core';
import { AngularFireAuth } from 'angularfire2/auth';
@Injectable()
export class AccountService {
account = {} as Account;
constructor(private afAuth: AngularFireAuth) {
}
login() {
console.log(this.account.email); // *** returns unidentified
return this.account.email;
// do login here with email and password
}
setID(val) {
this.account.uid = val;
}
setEmail(val) {
this.account.email = val;
}
setPassword(val) {
this.account.password = val;
}
setLoginfo(val) {
this.account.logInfo = val;
}
getID() {
return this.account.uid;
}
getEmail() {
return this.account.email;
}
getPassword() {
return this.account.password;
}
getLoginfo() {
return this.account.logInfo;
}
}