我有 dashboard.html 页面,DashboardController.js 与 dashboard.html 相关联。我正在 UIWebview 中加载页面,现在我想从 uiwebview 的 DashboardController.js 调用一个 angularjs 函数。它不会被调用。
下面是我在 iOS 中尝试的代码
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = [request URL];
NSLog(@"URL:%@",url);
if (![Utility isInternetAvailable]) {
return FALSE;
}
NSString *jsCallBack = [NSString stringWithFormat:@"DashboardController.test()"];
NSString *response = [webView stringByEvaluatingJavaScriptFromString:jsCallBack];
NSLog(@"%@",response);
return YES; // Return YES to make sure regular navigation works as expected.
}
在确保 dashboard.html 已加载到 uiwebview 中后,我还尝试了不使用 Controller 引用。 NSString *jsCallBack = [NSString stringWithFormat:@"test()"];
虽然 test() 是我的 DashboardController.js 范围内的 js 函数。
function test (){
alert('sendCredentialsToNativeApp');
}
提前致谢。
最佳答案
如果您在 Controller JS 中定义时没有 Controller 引用,则应调用此 test() 方法。但是,这仅在页面就绪状态后可用,因为在 Controller 完全加载之前您无法从 Controller 调用任何方法。
关于javascript - 从 ios uiwebview 调用 AngularJS Controller 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25161579/