InAppBrowser executeScript回调根本不触发 [英] InAppBrowser executeScript callback not firing at all

查看:166
本文介绍了InAppBrowser executeScript回调根本不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ionic和ngCordova创建一个Hybrid应用程序.在应用程序登录工作流程中,我正在从服务器返回一个页面到InAppBrowser插件.我需要从Ionic应用程序的返回页面中读取一些值.

I am creating building a Hybrid application using Ionic with ngCordova. In the application login workflow I am returning a page from server to the InAppBrowser plug-in. I need to read some values from the returned page in my Ionic app.

通常的方法是在"loadstop"事件上调用executeScript,并在该脚本返回某些内容时设置回调.正如您在以下代码中看到的那样,我具有该设置.但是,无论我如何尝试,iOS Emulator,Android Emulator的回调都不会触发.

The usual way would be to call executeScript on 'loadstop' event, and setup a call back when that script returns something. And I have that setup as you can see in the following code. However, no matter what I try, iOS Emulator, Android Emulator the callback is not firing.

这是在InAppBrowser中打开Goog​​le网站的页面.

Here is the page that opens up the google website in InAppBrowser.

 $cordovaInAppBrowser.open('http://google.com', '_blank', options)
    .then(function (event) {
      //alert('succed')
    })
    .catch(function (event) {
      //alert('errored')
    });

然后是loadstop事件处理程序. 1 + 1脚本被很好地注入,因为我确实看到了带有2的警报.但是,回调函数根本没有被调用!!

And following is the loadstop event handler. The 1+1 script is being injected fine because I do see the alert with 2. However the callback function is not being invoked at all !!

 $rootScope.$on('$cordovaInAppBrowser:loadstop', function (e, event) {
      $cordovaInAppBrowser.executeScript(
        {
          code: "alert(1+1);"
        },
        function (values) {
          alert('add completed');
        });
});

我尝试了多个仿真器,但是没有一个在工作.提前感谢您的帮助.

I gave multiple emulators a try but none of them are working. Appreciate your help in advance.

推荐答案

问题是executeScript本身是promise,因此,要使您的代码正常工作,您必须编写以下内容:

The problem is that executeScript is a promise itself, so to make your code working you must write something like this:

$rootScope.$on('$cordovaInAppBrowser:loadstop', function (e, event) {
  $cordovaInAppBrowser.executeScript(
    {
      code: "alert(1+1);"
    }).then(
        function (values) {
             alert('add completed');
        });
    });

这篇关于InAppBrowser executeScript回调根本不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆