How to call constructor in the method using ionic2? - ionic2

I am new to ionic 2, we want to call a constructor within the method how can I call
export class OtpPage {
constructor(public navCtrl: NavController, public navParams: NavParams,
public formBuilder:FormBuilder, public logger: Logger,
public rest: Rest) {
this.customer_id=this.navParams.get('customer_id');
this.mobile=this.navParams.get('mobile');
this.myForm = formBuilder.group({
'otpNumber': ['', Validators.required]
});
submit() {
if(result.status=='1'){
this.firstDiv=false;
this.secondDiv = true;
this.logger.debug("checking access tocken "+
this.access_token);
alert("otp success");
}
}
within these if loop how can I call constructor when the this.secondDiv=true

Related

Why is the service not injected into the controller?

I'm trying out AdonisJS, but I'm stuck trying to get a service injected into a controller. No matter what I try, the hs constructor argument in VersionsController remains undefined.
I've also experimented with annotating the VersionController constructor with #inject(['#ioc:Service/HashService']), but with no luck. I'm not sure though if #inject is the right way for Adonis.js v5.
How do I properly inject a service class into a controller?
/providers/AppProvider.ts
import { ApplicationContract } from '#ioc:Adonis/Core/Application'
import HashService from 'App/Services/HashService';
export default class AppProvider {
protected app: ApplicationContract;
constructor(app: ApplicationContract) {
this.app = app;
}
public register() {
this.app.container.singleton('#ioc:Service/HashService', () => {
return new HashService();
});
}
public async boot() {
// IoC container is ready
}
public async ready() {
// App is ready
}
public async shutdown() {
// Cleanup, since app is going down
}
}
/app/Services/HashService.ts
'use strict'
export default class HashService {
async test() {}
}
module.exports = HashService;
app/Controllers/Http/VersionsController.ts
import { HttpContextContract } from '#ioc:Adonis/Core/HttpContext'
import { HashService } from '#ioc:Service/HashService'
export default class VersionsController {
protected hs: HashService
constructor(hs: HashService) {
this.hs = hs;
console.log("hashservice is " + hs);
}
public async get(ctx: HttpContextContract) {
return [
{
id: 1,
title: 'a'
}
]
}
public async put(ctx: HttpContextContract) {
return "PUT";
}
}
.adonisrc.json
{
"typescript": true,
"commands": [
"./commands",
"#adonisjs/core/build/commands/index.js",
"#adonisjs/repl/build/commands"
],
"exceptionHandlerNamespace": "App/Exceptions/Handler",
"aliases": {
"App": "app",
"Config": "config",
"Database": "database",
"Contracts": "contracts"
},
"preloads": [
"./start/routes",
"./start/kernel"
],
"providers": [
"./providers/AppProvider",
"#adonisjs/core"
],
"aceProviders": [
"#adonisjs/repl"
]
}
you can use Adonisjs/fold
simply use the #inject decorator in your services;
example;
import {HttpContextContract} from "#ioc:Adonis/Core/HttpContext";
import UserEditValidator from "App/Validators/UserEditValidator";
import UserRepository from "App/Repository/UserRepository";
import {inject} from "#adonisjs/fold";
#inject()
export default class UserService {
constructor(private userRepository: UserRepository) {
}
async update(ctx: HttpContextContract) {
//do your stuff
}
}
and then in your controller;
import {HttpContextContract} from "#ioc:Adonis/Core/HttpContext";
import UserService from "App/Service/UserService";
import {inject} from "#adonisjs/fold";
#inject()
export default class UserController {
constructor(private userService: UserService) {
}
async edit({ view }){
return view.render('user/edit', {title: 'Edit Profile'})
}
async update(ctx : HttpContextContract){
await this.userService.update(ctx);
return ctx.response.redirect().back()
}
}

i want to retain the value of radio button when open and close the modal in Ionic2 using modalcontroller

import { Component } from '#angular/core';
import { IonicPage, NavController, NavParams} from 'ionic-angular';
import { FormGroup, FormControl} from '#angular/forms';
import { ViewController} from 'ionic-angular';
import { ModalController } from 'ionic-angular';
import { TabsPage } from "../tabs/tabs"
#IonicPage()
#Component({
selector: 'page-dashboard',
templateUrl: 'listing.html'
})
export class ListingPage {
constructor(public navCtrl: NavController, public navParams: NavParams, public modalCtrl: ModalController) {
}
ionViewDidLoad() {
console.log('ionViewDidLoad Dashboard');
}
//modals started
filterSort() {
let modal = this.modalCtrl.create(SortModal);
modal.present();
}
}
#Component({
selector:"sort-page",
templateUrl:"filter/sort.listing.filter.html",
providers:[ListingPage]
})
export class SortModal {
relationship:string;
filtersStore:Array<string>=[];
public myForm: FormGroup;
constructor(private _filterservice:FilterModalsService,private _v:ViewController,private _sharedservice: SharedService,private listingpage:ListingPage,public navCtrl: NavController){
this.myForm = new FormGroup({
langs: new FormControl()
})
}
doSubmit() {
this.listingpage.onSearch(0,true);
this._v.dismiss();
}
reset(){
this.myForm.reset();
this._sharedservice.deleteData();
}
}
listingpage is parent component in which i open the SortModal component as modal in ionic 2 and child is SortModal but when i open the modal by defualt every element is reset and then i select one from the list and dismiss the modal but when i open the modal again all value again empty. i want retain value the element of radio which i selected before

ionic2 ModalController can not show in websocket

import { Component } from '#angular/core';
import { NavController, Events, ModalController } from 'ionic-angular';
import * as SockJS from 'sockjs-client';
import { SomePage } from './somepage';
export class DashboardPage {
sockjs: any;
constructor(public navCtrl: NavController,
public events: Events,
private modalCtrl: ModalController) {
this.initWebsocket();
}
initWebsocket() {
this.sockjs = new SockJS('http://192.168.0.141:8080/ws-driver');
this.sockjs.onopen = function () {
console.log('open');
};
this.sockjs.onmessage = function (e) {
console.log('message', e.data);
alert('ok');
let model = this.modalCtrl.create(SomePage);
model.present();
};
this.sockjs.onclose = function () {
console.log('close');
};
}
}
alert('ok') had been process.
thanks
Use arrow functions instead of function keyword as this within the function refers to function object.
Try:
this.sockjs.onmessage = (e)=> {
console.log('message', e.data);
alert('ok');
let model = this.modalCtrl.create(SomePage);
model.present();
};
Or set self=this;
let self = this;
this.sockjs.onmessage = function (e) {
console.log('message', e.data);
alert('ok');
let model = self.modalCtrl.create(SomePage);
model.present();
};

encrypt a password using pbkdf2-sha256 in ionic2

how to encrpt password in ionic2 typescript file.I refered the below link and imported pbkdf2 module in to ionic2 app.
enter link description here
but i got error like webpack_require.i(...) is not a function.can anyone help me solving out this...
import { Component } from '#angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { pbkdf2 } from '../../../node_modules/pbkdf2-sha256/lib/pbkdf2.js';
/*
Generated class for the Firstpage page.
See http://ionicframework.com/docs/v2/components/#navigation for more info on
Ionic pages and navigation.
*/
#Component({
selector: 'page-firstpage',
templateUrl: 'firstpage.html'
})
export class FirstpagePage {
key : any = "passwd";
salt :any ="salt";
res :any ;
constructor(public navCtrl: NavController, public navParams: NavParams) {}
ionViewDidLoad() {
console.log('ionViewDidLoad FirstpagePage');
}
encrypt(){
console.log("-----");
this.res = pbkdf2(this.key, this.salt, 1, 64);
console.log(this.res.toString('hex'))
}
}
Try to define pbkdf2 in the constructor.
constructor(public navCtrl: NavController, public navParams: NavParams,
public crypt:pbkdf2) {}

Ionic 2: How to call Provider function in Controller class

I have created new Cordova Plugin only on my machine. Then I added it to my project. It is working fine when I call that plugin. Now, I tried to make a structured caller for my plugin. I created a Provider for it, but the problem is I don't know how to call my plugin function from my Controller class. Below is my sample code.
Provider: my-service.ts
import { Injectable } from '#angular/core';
import { Http } from '#angular/http';
import 'rxjs/add/operator/map';
declare let myPlugin: any;
#Injectable()
export class MyService {
constructor(public http: Http) {
console.log('Hello MyService Provider');
}
public myFunction() {
myPlugin.myPluginFunction(
(data) => {
return data;
},
(err) => {
return err;
});
}
}
Pages: my-page.ts
import { Component } from '#angular/core';
import { NavController, ViewController } from 'ionic-angular';
import { MyService } from '../../providers/my-service';
#Component({
selector: 'page-my-page-ionic',
templateUrl: 'hello-ionic.html'
})
export class MyPage {
constructor(private viewCtrl: ViewController, private myService: MyService) {}
ionViewWillEnter() {
//I tried to call like this
this.myService.myFunction().subscribe(
data => {
alert("success");
},
error => {
alert("error");
});
}
}
It returns me this error - Property 'subscribe' does not exist on type 'void'. I don't know how to call that function, since my provider returns me success or error.
I think since your myFunction() does not return any observable you cannot subscribe to it. It just returns data directly.
You can use it like this in this case:
var data = this.myService.myFunction();
console.log("Data from plugin is :", data);
If you want to use it as an Observable, return a new observable like this:
public myFunction() {
return Observable.create(observer => {
myPlugin.myPluginFunction(
(data) => {
observer.next(data);
},
(err) => {
observer.next(data);
});
},
(err) => {
observer.error(err);
});
}