gpt4 book ai didi

javascript - 在 Angularjs 应用程序中从 iOS Webview 调用 Javascript 函数

转载 作者:搜寻专家 更新时间:2023-10-30 20:00:06 30 4
gpt4 key购买 nike

我的问题是,当我尝试从 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 Controller :

'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 Controller 函数中可用。要使其全局可用,您需要将 Controller 修改为如下所示:

'use strict';

angular.module('testing.controllers', [])

.controller('Testing', function($scope) {

window.testing = function() {
alert('testing');
}

});

虽然不能说我建议你这样做,但很难判断,因为我们不知道你为什么要这样做。

关于javascript - 在 Angularjs 应用程序中从 iOS Webview 调用 Javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22407062/

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