Embedded YouTube video not working in Ionic3 lazy-loaded page - ionic-framework

I'm trying to embed a YouTube video through their iframe API in Ionic 3 app on a lazy-loaded page.
sample.ts
import { Component } from '#angular/core';
import { NavController } from 'ionic-angular';
import { IonicPage } from 'ionic-angular';
#IonicPage({
name: 'samplePage',
segment: 'sample'
})
#Component({
selector: 'page-sample',
templateUrl: 'sample.html'
})
export class SamplePage {
constructor(public navCtrl: NavController) {
}
}
sample.module.ts
import {NgModule} from '#angular/core';
import {IonicPageModule} from 'ionic-angular';
import { SamplePage } from './sample';
#NgModule({
declarations: [
SamplePage
],
imports: [
IonicPageModule.forChild(SamplePage)
],
exports: [
SamplePage
]
})
export class SamplePageModule {
}
sample.html
<iframe width="560" height="315" src="https://www.youtube.com/embed/videoID" frameborder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
I was expecting that this YouTube video will work in all the browsers but this is random.
The video is not working on Chrome browser but when I open the developer mode or change the screen size it works.
This is only happening when I'm adding it to a lazy loaded page using #IonicPage() annotation but when I remove this annotation and use the page normally, that works fine everywhere.

Related

I'm having the following error with my ionic application when working with the BarcodeScanner

I am developing an application to read codes that I am using the BarcodeScanner but when I execute the application on my device I get the following error
**
**Error running it on my iphone 6 using ionic DevApp Error Runtime Error Object(_WEBPACK_IMPORTED_MODULE_1_ionic_native_core_["cordova"])is not a function. (In 'Object(_WEBPACK_IMPORTED_MODULE_1_ionic_native_core_["cordova"])(this, "scan", {"callbackOrder":"reverse"}, arguments)', 'Object(_WEBPACK_IMPORTED_MODULE_1_ionic_native_core_["cordova"])'is an instance of Object)
Error that is displayed when executing it in ionic serve, ERROR TypeError: Object(...) is not a function at BarcodeScanner.scan (index.js:31) at MenuPage.webpackJsonp.101.MenuPage.scanQR (menu.ts:53) at Object.eval [as handleEvent] (MenuPage.html:17) at handleEvent (core.js:13589) at callWithDebugContext (core.js:15098) at Object.debugHandleEvent [as handleEvent] (core.js:14685) at dispatchEvent (core.js:10004) at core.js:10629 at HTMLButtonElement. (platform-browser.js:2628) at t.invokeTask (polyfills.js:3) **
at the beginning I had the error that my app-module.ts did not
recognize me the BarcodeScanner because when calling it in providers
me, TS2322: Type 'BarcodeScannerOriginal' is not assignable to type
'Provider'. Type 'BarcodeScannerOriginal' is not assignable to type
'FactoryProvider'. Property 'provide' is missing in type
'BarcodeScannerOriginal'
**
Menu.html
<ion-header class="toolbar">
<ion-navbar>
<ion-title>Scan</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding class="Scan">
<div class="row">
<div class="col">
<h2>Scan your QR Code Here</h2>
</div>
<div class="col">
<h3>{{eventTitle}}</h3>
</div>
</div>
<button ion-button block color="secondary" class="Scan-button" (click)="scanQR()" [disabled]="loading">{{buttonText}}</button>
</ion-content>
Menu.ts
import { Component } from '#angular/core';
import {Platform} from "ionic-angular";
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { SplashScreen } from '#ionic-native/splash-screen';
import { StatusBar } from '#ionic-native/status-bar';
import { ToastController } from 'ionic-angular';
import { BarcodeScanner, BarcodeScannerOptions } from '#ionic-native/barcode-scanner/ngx';
/**
* Generated class for the MenuPage page.
*
* See https://ionicframework.com/docs/components/#navigation for more info on
* Ionic pages and navigation.
*/
#IonicPage()
#Component({
selector: 'page-menu',
templateUrl: 'menu.html',
})
export class MenuPage {
public scannedText: string;
public buttonText: string;
public loading: boolean;
private eventId: number;
public eventTitle: string;
num: string;
// #ts-ignore
constructor(private _nav: NavController,
private _navParams: NavParams,
private _barcodeScanner: BarcodeScanner) {
}
ionViewDidLoad() {
this.eventId = this._navParams.get('eventId');
this.eventTitle = this._navParams.get('eventTitle');
this.buttonText = "Scan";
this.loading = false;
}
public scanQR() {
this.buttonText = "Loading..";
this.loading = true;
this._barcodeScanner.scan().then((barcodeData) => {
if (barcodeData.cancelled) {
console.log("User cancelled the action!");
this.buttonText = "Scan";
this.loading = false;
return false;
}
console.log("Scanned successfully!");
console.log(barcodeData);
this.goToResult(barcodeData);
}, (err) => {
console.log(err);
});
}
private goToResult(barcodeData) {
this._nav.push(ScanResultPage, {
scannedText: barcodeData.text
});
}
}
app-module.ts
import { BrowserModule } from '#angular/platform-browser';
import { ErrorHandler, NgModule } from '#angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '#ionic-native/splash-screen';
import { StatusBar } from '#ionic-native/status-bar';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import {MenuPage} from "../pages/menu/menu";
import { BarcodeScanner } from '#ionic-native/barcode-scanner/ngx';
import { HttpClientModule } from '#angular/common/http';
import { FormsModule } from '#angular/forms';
import {HttpModule} from "#angular/http";
// #ts-ignore
#NgModule({
declarations: [
MyApp,
HomePage,
MenuPage
],
imports: [
BrowserModule, HttpClientModule,
IonicModule.forRoot(MyApp),
HttpModule
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage,
MenuPage
],
providers: [
StatusBar,
SplashScreen,
BarcodeScanner,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}
enter image description here
My guess is you are on Ionic 3 but you are using the native plugin version supported for Ionic 4.
Solution
Uninstall both cordova and ionic native plugin
$ionic cordova plugin remove phonegap-plugin-barcodescanner
$npm uninstall #ionic-native/barcode-scanner
Install version 4.
$ ionic cordova plugin add phonegap-plugin-barcodescanner
$ npm install --save #ionic-native/barcode-scanner#4
And don't append ngx at the end of the import.
import { BarcodeScanner } from '#ionic-native/barcode-scanner';
Note
If you are using Ionic 3, try following the Ionic v3 guide instead of the latest guide
Ionic v3 guide:https://ionicframework.com/docs/v3/native/barcode-scanner/
For complete explanation you can find my other answer here: https://stackoverflow.com/a/54474247/6617276

Ionic - Runtime Error this.fba.logEvent(...).then is not a function

I am using Ionic and Firebase. I have created firebase project, clicked on icon to "add fire to your android app" and followed the steps.
Foll. is code on my html page:
<ion-header>
<ion-navbar>
<ion-title>
Ionic Blank
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
Firebase Analytics
<p>
Click the button to log a custom event
</p>
<button ion-button full (click)="logClick()">Log event</button>
</ion-content>
Below code is on the ts of this page:
import { Component } from '#angular/core';
import { NavController } from 'ionic-angular';
import { EventLoggerProvider } from '../../providers/event-logger/event-logger';
#Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor(public navCtrl: NavController,
public logger: EventLoggerProvider) {
}
logClick() {
this.logger.logButton('homeButton',{ pram: "paramValue" })
}
}
Foll. code is in the provider:
import { HttpClient } from '#angular/common/http';
import { Injectable } from '#angular/core';
import { FirebaseAnalytics } from '#ionic-native/firebase-analytics';
#Injectable()
export class EventLoggerProvider {
constructor(public fba: FirebaseAnalytics) {
console.log('Hello EventLoggerProvider Provider');
}
logButton(name:string,value:any){
this.fba.logEvent(name, { pram:value })
.then((res: any) => {console.log(res);})
.catch((error: any) => console.error(error));
}
}
I ran "ionic cordova run android" and launched the app on mobile device and everything seems to be OK. However, when I do "ionic serve", the page loads OK in the browser, but when I click the button, what could have caused it to give the foll. error?
Ionic - Runtime Error this.fba.logEvent(...).then is not a function
Now its more clear after seing the code.
import { FirebaseAnalytics } from '#ionic-native/firebase-analytics';
#ionic-native functions only work on the real device or simulator. Otherwise, it will give an error.

How to implement rating in ionic 3?

How do I create 5 star rating in ionic 3, I visited https://github.com/andrucz/ionic2-rating but it is not working. Also read some tutorial but not found anything clean and clear for ionic 3.
Just for clear, I want to get rating value and display rating.
Any help would be appreciated.
Thankyou
Demo: https://stackblitz.com/edit/ionic3-star-rating
Use github.com/melwinVincent/ionic3-star-rating npm package.
Step 1. Add the ionic3-star-rating component in your page (parent component) as follows
<ionic3-star-rating
activeIcon = "ios-star"
defaultIcon = "ios-star-outline"
activeColor = "#488aff"
defaultColor = "#f4f4f4"
readonly="false"
[rating]="3">
</ionic3-star-rating>
Step 2.
You have to import the StarRatingModule in the module.ts of your parent component as follows
import { NgModule } from '#angular/core';
import { IonicPageModule } from 'ionic-angular';
import { ParentPage } from './parent';
import { StarRatingModule } from 'ionic3-star-rating';
#NgModule({
declarations: [
ParentPage,
],
imports: [
StarRatingModule,
IonicPageModule.forChild(ParentPage),
],
})
export class ParentPageModule {}
Step 3.
To get the changed rating in the parent component,
You need to use events from ionic package.
import { Component } from '#angular/core';
import { NavController } from 'ionic-angular';
import { Events } from 'ionic-angular';
#Component({
selector: 'page-about',
templateUrl: 'about.html'
})
export class AboutPage {
constructor(public navCtrl: NavController,
public events: Events) {
events.subscribe('star-rating:changed', (starRating) => {console.log(starRating)});
}
}

Multiple component in shared.module.ts Ionic

i am new to ionic and recently i tried to create a shared component so that i can sort of remove duplicate coding of same content on different page. It consist of a notification bar and a panel that have a login/signup button. I am able to display the notification bar and the login panel on pages that i want but i am unable to call the function of the login/signup button.
Whenever i click the login/signup button, it will said presentLoginModal() is not a function. Below are my code.
Hope someone can advice me on this. Thanks.
shared.module.ts
import {NgModule} from '#angular/core';
import {IonicPageModule} from 'ionic-angular';
import {NotificationComponent} from '../notification/notification';
import {NonLoginComponent} from '../nonlogin/nonlogin';
#NgModule({
imports: [IonicPageModule.forChild(NotificationComponent),IonicPageModule.forChild(NonLoginComponent)],
declarations: [NotificationComponent, NonLoginComponent],
exports: [NotificationComponent, NonLoginComponent]
}) export class SharedModule {}
nonlogin.ts
import { Component } from '#angular/core';
import { NavController } from 'ionic-angular';
import {Login} from '../login/login';
import {Register} from '../register/register'
#IonicPage()
#Component({
selector: 'app-nonlogin',
templateUrl: './nonlogin.html'
})
export class NonLoginComponent {
constructor(public navCtrl: NavController) {}
presentLoginModal() {
let loginModal = this.modalCtrl.create(Login, {showBackdrop: true, enableBackdropDismiss: true},{cssClass: 'logon'});
loginModal.present();
}
presentRegisterModal(){
let registerModal = this.modalCtrl.create(Register, {showBackdrop: true, enableBackdropDismiss: true},{cssClass: 'register'});
registerModal.present();
}
}
notification.ts
import { Component } from '#angular/core';
import { NavController } from 'ionic-angular';
#Component({
selector: 'app-notification',
templateUrl: './notification.html'
})
export class NotificationComponent {
constructor(public navCtrl: NavController) {}
}
nonlogin.html
<div class="toppanel"><div class="leftside"><img class="companylogo"
src="../../assets/imgs/logo.jpg"></div><div class="rightside">
<button tappable class="login" ion-button
(click)="presentLoginModal()">LOG IN</button><button tappable
class="signup" ion-button (click)="presentRegisterModal()">SIGN
UP</button></div></div>
home.html
<ion-content>
<app-notification></app-notification>
<app-nonlogin></app-nonlogin>
</ion-content>

how to get realtime JSON from endpoint in Ionic App

Followed the content of the url to implement dynamic menu items using JSON file stored under /assets/data. The menu is working fine with stored JSON file. Now I need to dynamically retrieve the JSON of same format in real time from a Salesforce API and display its content.
Can someone please suggest what changes I need to make here? should the json path in getMainMenu() method be replaced with the actual Saleforce API?
Below is the data-service.ts
import { Injectable } from '#angular/core';
import { Http, Response } from '#angular/http';
import 'rxjs/add/operator/map';
/*
Generated class for the DataServiceProvider provider.
See https://angular.io/guide/dependency-injection for more info on providers
and Angular DI.
*/
#Injectable()
export class DataServiceProvider {
constructor(public http: Http) {
console.log('Hello DataServiceProvider Provider');
}
getMainMenu(){
return this.http.get('assets/data/mainmenu.json')
.map((response:Response)=>response.json().Categories);
}
}
and app.component.ts
import { Component, ViewChild } from '#angular/core';
import { Nav, Platform } from 'ionic-angular';
import { StatusBar } from '#ionic-native/status-bar';
import { SplashScreen } from '#ionic-native/splash-screen';
import { HomePage } from '../pages/home/home';
import { ListPage } from '../pages/list/list';
import { DataServiceProvider } from '../providers/data-service/data-service'
#Component({
templateUrl: 'app.html'
})
export class MyApp {
#ViewChild(Nav) nav: Nav;
rootPage: any = HomePage;
pages: any[]; //Array<{title: string, component: any}>;
mainmenu: any[];
constructor(public platform: Platform, public statusBar: StatusBar, public splashScreen: SplashScreen, public dataService: DataServiceProvider) {
this.initializeApp();
this.dataService.getMainMenu().subscribe((Response)=>{
this.mainmenu = Response;
console.log(this.mainmenu);
});
// used for an example of ngFor and navigation
this.pages = [
{ title: 'Home', component: HomePage },
{ title: 'List', component: ListPage }
];
}
initializeApp() {
this.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.
this.statusBar.styleDefault();
this.splashScreen.hide();
});
}
openPage(page) {
// Reset the content nav to have just this page
// we wouldn't want the back button to show in this scenario
this.nav.setRoot(page.component);
}
toggleSection(i) {
this.mainmenu[i].open = !this.mainmenu[i].open;
};
toggleItem(i,j) {
this.mainmenu[i].SubCategories[j].open = !this.mainmenu[i].SubCategories[j].open;
};
}
It looks like you will need to update the url in the getMainMenu method to that of your api. There might be some other changes you will need to make, such as adding authentication headers, but if the data coming from the api is the same as whats stored in the assets folder, your component shouldn't care "where" the data comes from.