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中打开Google网站的页面.
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');
});
});