我的问题是,当我尝试从 UIWebView 中调用 AngularJS 应用程序中存在的 javascript 函数时,该函数无法识别。当我在典型的 html 结构中调用该函数时,该函数会按预期被识别。下面提供的示例:
目标-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(适用于当前的 Objective-C):
<!DOCTYPE HTML>
<html>
<body>
<SCRIPT>
function testing() {
alert('testing');
}
</SCRIPT>
</body>
</html>
这是因为你的testing
该功能并非全局可用。它只能在您的内部使用Testing
控制器功能。为了使其全局可用,您需要将控制器修改为如下所示:
'use strict';
angular.module('testing.controllers', [])
.controller('Testing', function($scope) {
window.testing = function() {
alert('testing');
}
});
虽然不能说我建议您这样做,但很难判断,因为我们不知道您为什么要这样做。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)