Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access typescript methods from javascript in ionic2

I'm tyring to call a typescript method from javascript in ionic2. Let's consider a ionic2 page generated by "ionic g page MyPage" where I have created a method myNewMethod".

Is there any way to call myNewMethod" from a javascript function which is part of a cordova plugin? I've noticed that in main.js, as transpiled in javascripte, there is a variable MyPage and myNewMethod is defined as prototype. However If I call MyPage.myNewMethod doesn't seem to work.

I have further noticed the following part of code:

/* 312 */
(function(module, __webpack_exports__, __webpack_require__) {

"use strict";
var __WEBPACK_IMPORTED_MODULE_0__angular_core__ = __webpack_require__(0);
var __WEBPACK_IMPORTED_MODULE_1_ionic_angular__ = __webpack_require__(136);
__webpack_require__.d(__webpack_exports__, "a", function() { return MyPage; });
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};


var MyPage = (function () {
    function MyPage(navCtrl, navParams) {
        this.navCtrl = navCtrl;
        this.navParams = navParams;
    }
    MyPage.prototype.ionViewDidLoad = function () {
        console.log('ionViewDidLoad MyPage');
    };
    MyPage.prototype.myNewMethod = function () {
        //do stuff
    };
    MyPage = __decorate([
        __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__angular_core__["B"])({
            selector: 'page-add-item',template:'Some Template'
        }),
        __metadata('design:paramtypes', [__WEBPACK_IMPORTED_MODULE_1_ionic_angular__["d"], __WEBPACK_IMPORTED_MODULE_1_ionic_angular__["e"]])
    ], MyPage);
    return MyPage;
}());


}),

It seems that the scope of MyPage is not available directly and it's being returned through webpack_requare

Any help would be appreciated.

Thanks

like image 462
Symeon Mattes Avatar asked Jun 16 '26 04:06

Symeon Mattes


1 Answers

Probably it's not the best way since I make MyPage public, but at least it works. What I did is I assigned MyPage to window variable, i.e. during ionViewDidLoad I wrote:

eval('window.MyPage = this');

MyMethod is considered a prototype and as a consequence I can call it from javascript code through:

window.MyPage.MyMethod();
like image 136
Symeon Mattes Avatar answered Jun 17 '26 18:06

Symeon Mattes