我的问题是当我尝试从UIWebView中调用我的AngularJS应用程序中存在的javascript函数时,该函数无法识别.当我在典型的html结构中调用该函数时,该函数被识别为预期的.示例如下:
Objective-C的:
- (void)viewDidLoad
{
[super viewDidLoad];
// CODE GOES HERE
_webView.delegate = self;
// LOAD THE CHABACORP HOMEPAGE WITH INITIAL UIWEBVIEW
NSString *fullURL = @"http://testing.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[_webView stringByEvaluatingJavaScriptFromString:@"testing()"];
}
ANGULARJS控制器:
'use strict';
angular.module('testing.controllers', [])
.controller('Testing', function($scope) {
function testing() {
alert('testing');
}
});
基本HTML(与当前目标-C一起使用):
<!DOCTYPE HTML>
<html>
<body>
<SCRIPT>
function testing() {
alert('testing');
}
</SCRIPT>
</body>
</html>
解决方法:
这是因为您的测试功能不是全局可用的.它仅在您的测试控制器功能中可用.要使其全局可用,您需要将控制器修改为如下所示:
'use strict';
angular.module('testing.controllers', [])
.controller('Testing', function($scope) {
window.testing = function() {
alert('testing');
}
});
不能说我建议你这样做,但很难判断,因为我们不知道你为什么要这样做.
原文地址:https://codeday.me/bug/20190825/1716400.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。