Impossible to use ion-slides methods (No provider for ChangeDetectorRef) - ionic-framework

I want to use ion-slides method getActiveIndex() but when I import ion-slides as a provider, I have this error :
NullInjectorError: No provider for ChangeDetectorRef!
I don't know why I have to add this to providers. Even if I add this, I have the other provider: ElementRef...
If you have a look at my code, could you tell me if I'm doing something wrong?
thanks!
liste.page.ts :
import {IonSlides} from '#ionic/angular';
#Component({
selector: 'app-liste',
templateUrl: './liste.page.html',
styleUrls: ['./liste.page.scss'],
})
export class ListePage {
constructor(private ionSlides: IonSlides) { }
slideChanged() {
console.log(this.ionSlides.getActiveIndex());
}
}
liste.page.html:
<ion-slides [options]="sliderConfig" (ionSlideTransitionEnd)="slideChanged()">
<ion-slide *ngFor="let item of items; let i = index">
{{item.name}}
</ion-slide>
</ion-slides
Ionic Info:
ionic (Ionic CLI) : 4.9.0
Ionic Framework : #ionic/angular 4.1.2
#angular-devkit/build-angular : 0.13.6
#angular-devkit/schematics : 7.2.4
#angular/cli : 7.3.6
#ionic/angular-toolkit : 1.4.1

In your liste.page.html use:
<ion-slides pager="true" #slides>
Then you can reference it in your liste.page.ts file with:
import {IonSlides} from '#ionic/angular';
import {ViewChild} from '#angular/core'; // <----
[...]
export class ListePage {
#ViewChild('slides', { read: IonSlides }) slides: IonSlides; // <----
Then you have access to slides and can call getActiveIndex();
slideChanged() {
console.log(this.slides.getActiveIndex());
}

Below worked for me
import {IonSlides} from '#ionic/angular';
import {ViewChild} from '#angular/core'; // <----
[...]
export class ListePage {
#ViewChild('sliderRef', { static: true }) slides: IonSlides;
slideChanged(){
this.slides.getActiveIndex().then((val) =>{
console.log(val);
});
<ion-slides #sliderRef pager="true" [options]="slideOpts" (ionSlideDidChange)="slideChanged()" >

Related

Ionic backgorund mode crash

I am trying to make run a background mode with Ionic, but when I am playing a song, and I enabled the Backgound Mode, the app crashes.
Actually, is posible make the background mode with Ionic?
This is my code:
import { Component } from '#angular/core';
import {NavController} from '#ionic/angular';
import {NativeAudio} from '#ionic-native/native-audio/ngx';
import {BackgroundMode} from '#ionic-native/background-mode/ngx';
#Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
constructor(public navCtrl: NavController,
public nativeAudio: NativeAudio ,
public backgroundMode: BackgroundMode) {
this.nativeAudio.preloadSimple('audio1', 'assets/audio1.mp3').then((msg) => {
console.log('message: ' + msg);
}, (error) => {
console.log('error: ' + error);
});
}
public playAudio(){
this.backgroundMode.enable();
this.backgroundMode.on('activate').subscribe(() => {
this.nativeAudio.play('audio1');
});
this.nativeAudio.play('audio1', () => console.log('audio1 is done playing'));
}
}
This is my current config.
/bg-mode-example$ ionic info
Ionic:
Ionic CLI : 6.11.11 (/usr/local/lib/node_modules/#ionic/cli)
Ionic Framework : #ionic/angular 5.3.5
#angular-devkit/build-angular : 0.1000.8
#angular-devkit/schematics : 10.0.8
#angular/cli : 10.0.8
#ionic/angular-toolkit : 2.3.3
Cordova:
Cordova CLI : 10.0.0
Cordova Platforms : android 9.0.0
Cordova Plugins : cordova-plugin-ionic-keyboard 2.2.0, cordova-plugin-ionic-webview 4.2.1, (and 6 other plugins)
Utility:
cordova-res (update available: 0.15.1) : 0.6.0
native-run (update available: 1.2.1) : 0.2.8
System:
Android SDK Tools : 26.1.1 (/home/gabriel/Android/Sdk)
NodeJS : v10.19.0 (/usr/bin/node)
npm : 6.14.8
OS : Linux 5.4
Addionationally, I have a gitlab repo https://gitlab.com/gabrielrincon/bgmode-testing/
What is wrong?
Thanks very much.
I can make it.
The problem is a permission. The FOREGROUND_SERVICE.
I add to platforms/android/app/src/main/AndroidManifest.xml and work
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
I hope that it help you

Can't use cordova-plugin-file on ionic 4

I am trying to use ionic 4 and cordova-plugin-file to get files from phone but it trows an error;
When i do console.log(cordova.file) like in the doc it shows cordova does not have property 'file'.
When i do
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail); it show 'requestFileSystem' not a property of window.
even if i do window.cordova it trow thesame error.
and unlike ionic 3 here if i add File to providers it trows an error as well
Please what is it i'm doing wrong?
Here is my ** home.page.ts**
import {Component} from '#angular/core';
import {Platform} from '#ionic/angular';
import {File} from '#ionic-native/file';
#Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
constructor(public platform: Platform) {
platform.ready().then(() => {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
console.log(fileSystem)
}, function(error) {
console.log(error);
});
});
}
}
For Ionic 4 you should add 'ngx' at the end of the import.
Like this,
import {File} from '#ionic-native/file/ngx';
Make sure to add it to the providers list of your module file, and also inject it to the constructor of the class where ever you are using the plugin.
Reference https://ionicframework.com/docs/native

Open External Link with Crosswalk Webview got Application Error

I want to make webview to show my webpage with crosswalk plugin. When I put my url, and click the button. I got error
"Application Error"
"net:ERR_INSECURE_RESPONSE
(https://example.dummy.com:8444)"
my home page is using https
this is my code :
home.html
<ion-slide>
<h2>Please, Enjoy to call with our customer service</h2>
<button ion-button block full (click)="goToPage('https://oscar2.kebhana.co.id:8444/gibPT/intn/info/call#359854')">Start Video Call</button>
</ion-slide>
home.ts
import { Component } from '#angular/core';
import { NavController } from 'ionic-angular';
#Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor(public navCtrl: NavController) {
}
goToPage(url : string) {
window.open(url, '_self');
}
}
Please help me to fix. Thank you
#anggaerlangga you can use InAppBrowser to open an external link. because IONIC is not a web application you have to use any browser for open external URL. I will show you how can you use the InAppBrowser.
first of all you have to install InAppBrowser NPM from the ionic native.
import { InAppBrowser} from '#ionic-native/in-app-browser';
let baseurl = 'your url';
let target = "_blank";
this.iab.create(baseurl,target);
Thank you. this will definitely help you.

Ionic import Camanjs

I'm trying to use camanjs library in my ionic v3 project. I installed it using npm install caman --save (also installed cairo, libjpg and libpng). Now in my project folder I can see a folder named caman in node_modules folder, so in my .ts I write
import { Component } from '#angular/core';
import { NavController } from 'ionic-angular';
import { Caman } from 'caman';
declare var Caman: any;
#Component({
selector: 'page-filtri',
templateUrl: 'filtri.html'
})
export class FiltriPage {
constructor(public navCtrl: NavController) {
}
addFilter(){
Caman("#image", function(){
this.sinCity();
this.render();
})
}
}
and the html file
<ion-header>
<ion-navbar>
<ion-title>
Ionic Blank
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<button ion-button (click)="addFilter()">Filtro</button>
<img id='image' src="https://firebasestorage....">
</ion-content>
but if I serve I've
ERROR ReferenceError: Caman is not defined
And that's how my node_modules folder looks like
[node_modules folder]
Can you help me?
Why don't you try changing your import to the following:
import * as Caman from 'caman';
You cannot declare both at the same time like this.
import { Caman } from 'caman';
declare var Caman: any;
you only need to use
import { Caman } from 'caman';
But you get the error "Module parse failed: ...." There is nothing wrong with the import it is because of the CamanJs Uses node-canvas. And canvas loads binary files. like "canvas.node". You need to install node-loader https://github.com/webpack-contrib/node-loader to resolve that error.

Angular 4 Data table implementation into my APP

I have created custom component DisplayTableComponent in my project. I want to incorporate Angular 4 Data table on my data for display purpose.
DisplayTableComponent.TS is as follows
import { Component, OnInit } from '#angular/core';
import { DataTableResource } from 'angular-4-data-table';
import { DataTableModule } from 'angular-4-data-table';
import persons from './data-table-demo1-data';
#Component({
selector: 'app-display-table',
templateUrl: './display-table.component.html',
styleUrls: ['./display-table.component.css']
})
export class DisplayTableComponent implements OnInit {
itemResource = new DataTableResource(persons);
items = [];
itemCount = 0;
constructor() {
this.itemResource.count().then(count => this.itemCount = count);
}
ngOnInit() {
}
reloadItems(params) {
// this.itemResource.query(params).then(items => this.items = items);
}
// special properties:
rowClick(rowEvent) {
console.log('Clicked: ' + rowEvent.row.item.name);
}
rowDoubleClick(rowEvent) {
alert('Double clicked: ' + rowEvent.row.item.name);
}
rowTooltip(item) { return item.jobTitle; }
}
My Html Template is as follows
<p>
display-table works!
</p>
<div style="margin: auto; max-width: 1000px; margin-bottom: 50px;">
<data-table id="persons-grid"
headerTitle="Employees"
[items]="items"
[itemCount]="itemCount"
(reload)="reloadItems($event)"
(rowClick)="rowClick($event)"
(rowDoubleClick)="rowDoubleClick($event)"
[rowTooltip]="rowTooltip"
>
<data-table-column
[property]="'name'"
[header]="'Name'"
[sortable]="true"
[resizable]="true">
</data-table-column>
<data-table-column
[property]="'date'"
[header]="'Date'"
[sortable]="true">
<ng-template #dataTableCell let-item="item">
<span>{{item.date | date:'yyyy-MM-dd'}}</span>
</ng-template>
</data-table-column>
<data-table-column
property="phoneNumber"
header="Phone number"
width="150px">
</data-table-column>
<data-table-column
[property]="'jobTitle'"
[header]="'Job title'"
[visible]="false">
</data-table-column>
<data-table-column
[property]="'active'"
[header]="'Active'"
[width]="100"
[resizable]="true">
<ng-template #dataTableHeader let-item="item">
<span style="color: rgb(232, 0, 0)">Active</span>
</ng-template>
<ng-template #dataTableCell let-item="item">
<span style="color: grey">
<span class="glyphicon glyphicon-ok" *ngIf="item.active"></span>
<span class="glyphicon glyphicon-remove" *ngIf="!item.active"></span>
</span>
</ng-template>
</data-table-column>
</data-table>
</div>
Now, The temporary source data file (data-table-demo1-data.ts) is as
export default [
{ 'name': 'Aaron 2Moore', 'email': 'aaa#aa.com', 'jobTitle': 'Regional Configuration Producer',
'active': true, 'phoneNumber': '611-898-6201', 'date': '2015-11-06T07:21:25.510Z' },
{ 'name': 'Yvonne Conroy Mrs.', 'email': 'sss#ssss.com', 'jobTitle': 'Global Mobility Orchestrator',
'active': false, 'phoneNumber': '115-850-0969', 'date': '2014-12-20T00:48:40.276Z' },
]
My app.Module.TS is as follows
import { BrowserModule } from '#angular/platform-browser';
import { NgModule,CUSTOM_ELEMENTS_SCHEMA } from '#angular/core';
import { Routes, RouterModule} from '#angular/router';
import { DataTableModule } from 'angular-4-data-table';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { MovieComponent } from './movie/movie.component';
import { DisplayTableComponent } from './display-table/display-table.component';
const appRoute: Routes =[
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{path:'home', component:HomeComponent},
{path:'Movie', component:MovieComponent},
{path:'table', component:DisplayTableComponent},
];
#NgModule({
declarations: [
AppComponent,
HomeComponent,
MovieComponent,
DisplayTableComponent
],
imports: [
BrowserModule,
RouterModule.forRoot(appRoute)
],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Could you please help me. I am getting below error
ERROR in ./node_modules/angular-4-data-table/src/index.ts
Module build failed: Error: C:\projects\Handson\website1\node_modules\angular-4-data-table\src\index.ts is missing from the TypeScript compilation. Please make sure
it is in your tsconfig via the 'files' or 'include' property.
The missing file seems to be part of a third party library. TS files in published libraries are often a sign of a badly packaged library. Please open an issue in the library repository to alert its author and ask them to package the library using the Angular Package Format
at AngularCompilerPlugin.getCompiledFile (C:\projects\Handson\website1\node_modules\#ngtools\webpack\src\angular_compiler_plugin.js:656:23)
at plugin.done.then (C:\projects\Handson\website1\node_modules\#ngtools\webpack\src\loader.js:467:39)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
# ./src/app/display-table/display-table.component.ts 13:29-60
# ./src/app/app.module.ts
# ./src/main.ts
# multi webpack-dev-server/client?http://0.0.0.0:0 ./src/main.ts
It seems you are using angular 5 cli and need to integrate Angular 4 Data table on to your project.
My advice is to use angular5-data-table instead of version 4 if you are using angular 5.You can find it on https://www.npmjs.com/package/angular5-data-table