如何调用在Android的WebView AngularJS服务code? [英] How to call AngularJS Service code from android webview?

查看:589
本文介绍了如何调用在Android的WebView AngularJS服务code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打电话给在Android的WebView组件AngularJS服务没有成功...
你有什么想法,我怎么可以这样?

I'm trying to call AngularJS service from android webview component without success... Do you have any idea how can I do this ?

这是java脚本code,我试图调用:

This is the java script code I'm trying to call:

view.loadUrl("javascript:$rootScope.myService.hello()");

先谢谢了。

推荐答案

除非你有的禁用调试数据的在您的应用程序的每个元素都有一个范围()函数可以调用找回该元素的范围。它也有一个喷油器,您可以使用取回喷油器实例您的应用程序()功能。所以,试试这个:

Unless you have disabled debug data then each element in your app has a scope() function you can call to get back the scope for that element. It also has an injector() function you can use to get back the injector instance for your app. So try this:

javascript:angular.element('[ng-app]').injector().get('myService').hello()

这可能会是更好的暴露只是你需要访问窗口,虽然该功能。

It would probably be better to expose just the function that you need access to on the window though.

所以,你需要做的是这样的:

So then you would need to do something like this:

angular.module('myApp').service('myService', function(yourDependencies..., $window) {
  //Your code 

  $window.hello = this.hello;
});

那么你的Andr​​oid code成为本:

Then your Android code becomes this:

view.loadUrl("javascript:hello()");

这篇关于如何调用在Android的WebView AngularJS服务code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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