尝试用android的webview注入执行一段js,但在前端使用angular.js的ng-view指令之后,注入的js里面的对象都变成了undefine


我在android webview 的onpagefinish函数里面加了一段这样的代码用于js注入:

   
  public void onPageFinished(WebView view, String url)
  
{
super.onPageFinished(view, url);
String jscode = "eval((function(){var abc="123";})";
view.loadUrl("javascript:" + jscode );
}

在前端用angular.js的ng-view指令以后,abc这个变量就变成了undefine.
如果不加<div ng-view></div> 这个abc就能alert出来

   
  <!doctype html>
  
<html lang="en" ng-app="app">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

<script src="js/lib/angular/angular.js"></script>
<script src="js/lib/angular/angular-route.js"></script>
<script src="js/lib/angular/angular-resource.js"></script>
<script src="js/lib/angular/angular-touch.js"></script>
<script src="js/lib/onsenui.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>

<script>
setTimeout(function(){
alert(abc);
},5000);
</script>
</head>
<body>

<div ng-view></div>

</body>
</html>

Android phonegap

原来你是过客 10 years, 4 months ago

Your Answer