Passing $ionicModal into `this` on a service? :C


#1

Having some issues passing $ionicModal into a variable to be used in my service.

angular.module(‘myModule’, [‘ionic’]).service(‘myService’, function($q, $ionicModal) {
this.modal = null;

this.init = function() {
    $ionicModal.fromTemplateUrl('my-modal.html').then(function(modal) {
        this.modal = modal;
    }).then(this.setup());
};

this.setup = function() {
    this.modal.show();
};

});

The code should work, but unfortunately it does not, shows error:

 Cannot read property 'show' of null

Strangely, if I am to just use modal.show() in the first promise it will display, but then I have no way to close it later. Help?


EDIT: I should add that I have a workaround with this issue (If anyone runs into it and a fix isn’t posted). Take your HTML file over to https://kangax.github.io/html-minifier/ and then uncheck the “Remove attribute quotes” box and click “minify”. Switch over to use $ionicModal.fromTemplate(string) and it will work fine in your service.