gpt4 book ai didi

javascript - 如何从 android webview 调用 AngularJS 服务代码?

转载 作者:行者123 更新时间:2023-11-29 21:36:35 27 4
gpt4 key购买 nike

我试图从 android webview 组件调用 AngularJS 服务但没有成功...你知道我该怎么做吗?

这是我要调用的 java 脚本代码:

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

提前致谢。

最佳答案

除非你有 disabled debug data那么您应用中的每个元素都有一个 scope() 函数,您可以调用该函数来取回该元素的作用域。它还有一个 injector() 函数,您可以使用它来取回应用程序的注入(inject)器实例。所以试试这个:

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

不过,最好只在窗口上公开您需要访问的功能。

那么你需要做这样的事情:

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

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

然后你的安卓代码变成这样:

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

关于javascript - 如何从 android webview 调用 AngularJS 服务代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34707637/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com