Call angularjs function from javascript

1

In an application that I am doing, I was asked to open the application from the browser. Install the plugin cordova-plugin-customurlscheme to define this and it works perfectly.

From the browser, I receive certain values when the application is opened, try to define the function handleOpenURL within the controller, but I receive the error:

  

Uncaught ReferenceError: handleOpenURL is not defined       at: 1: 1

I was investigating this error and it is solved adding it in the file index.html as a <script> staying like this:

<script>
    handleOpenURL = function (url) {
        setTimeout(function(){
            alert(url);
        },0);
    };
</script>

However, upon receiving the URL I would like to obtain the parameters, for example I receive the alert

miapp://?parametro=valor

And I would also like to handle events of angularjs as redirect or send an alert ionic , however I do not know if you can access a function of a controller outside of it, if this happens to be bad practice or some possible solution.

    
asked by sioesi 10.01.2017 в 13:01
source

1 answer

2

I am currently using the service you need in an Ionic App, I recommend you do it in the following way:

1 - Install the following plugin and modify testapp by the name you want:

    cordova plugin add https://github.com/EddyVerbruggen/LaunchMyApp-PhoneGap-Plugin.git \
 --variable URL_SCHEME=testapp

2 - In your app.js insert this snippet of code:

.run(['$state', '$window',
    function($state, $window) {
        $window.addEventListener('LaunchUrl', function(event) {
            // gets page name from url
            var page =/.*:[/]{2}([^?]*)[?]?(.*)/.exec(event.detail.url)[1];
            // redirects to page specified in url
            $state.go('app.'+ page, {});
        });
    }
]);

function handleOpenURL(url) {
    setTimeout( function() {
        var event = new CustomEvent('LaunchUrl', {detail: {'url': url}});
        window.dispatchEvent(event);
    }, 0);
}

3 - Add a status on your router provider:

.state('app.page, {
    url: '/page,
    views: {
        'menuContent': {
            templateUrl: 'templates/yourtempalte.html',
            controller: 'PageCtrl'
        }
    }
})

4 - From your web app call window.location = 'testapp: // parameter' and it should work.

You already tell us, regards!

    
answered by 10.01.2017 / 14:17
source