Search code examples
angularjscordovaionic-frameworkphonegap-pluginsinappbrowser

InAppBrowser Events handling issue


I am working on mobile app using:

  • Ionic 2.1.4
  • Cordova 6.4.0
  • Angular 1.5.3

I've one view with external url using InAppBrowser plugin and I have a link in this website should redirect to certain view in my app

this issue is $location.url() not redirecting and not working at all, but when I tested the event I found it trigger normally.

here is my full code

angular.module('yogipass').controller('iframe',function ($location) {
 document.addEventListener("deviceready", onDeviceReady, false);

 function onDeviceReady() {
  console.log('here');

  var ref=cordova.InAppBrowser.open('http://192.168.42.218/index.html', '_blank', 'location=no');

  ref.addEventListener('loadstart', function(event) {
   if (event.url.match("mobile/login")) {
    console.log('worked!') // this logged normally
    $location.url('/login');

      ref.close();
   }
  });




}

])

Solution

  • You have to run digest cycle manually as you are changing location path from asynchronous event which is outside angular context.

    Do wrap you code in $timeout function, which will fire up digest cycle. Apparently that will help to update location.

    $timeout(function(){
        $location.url('/login');
    })