调用Javascript函数从iOS Webview中的Angularjs应用程序 [英] Call Javascript Function From iOS Webview in Angularjs App

查看:205
本文介绍了调用Javascript函数从iOS Webview中的Angularjs应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,当我尝试调用存在于我的AngularJS应用程序从UIWebView中的JavaScript函数,该函数无法识别。当我在典型的html结构中调用函数时,函数被识别为期望的。下面提供的示例:



OBJECTIVE-C:

   - )viewDidLoad 
{
[super viewDidLoad];

//代码在这里
_webView.delegate = self;

//用初始UIWEBVIEW加载CHABACORP主页
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:

 '使用严格'; 

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

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

function testing(){
alert('testing');
}

});

BASIC HTML(与当前目标C配合工作):

 <!DOCTYPE HTML> 
< html>

< body>

< SCRIPT>
function testing(){
alert('testing');
}
< / SCRIPT>

< / body>

< / html>这是因为你的测试 / code>函数不是全局可用的。它仅在您的测试控制器功能内可用。要使其在全局可用,您需要修改控制器,像这样:

 'use strict'; 

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

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

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

});

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


My problem is that when I try to call a javascript function that exists in my AngularJS app from within a UIWebView that function is not recognized. When I call the function in a typical html structure the function is recognized as expected. Example provided below:

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');
    }

});

BASIC HTML (WORKS WITH CURRENT OBJECTIVE-C):

<!DOCTYPE HTML>
<html>

<body>

    <SCRIPT>
    function testing() {
        alert('testing');
    }
    </SCRIPT>

</body>

</html> 

解决方案

It's because your testing function isn't globally available. It's only available inside your Testing controller function. To make it globally available, you need to modify the controller to be something like this:

'use strict';

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

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

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

});

Can't say that I recommend you to do this though, but it's hard to judge since we don't know more about why you want to do this.

这篇关于调用Javascript函数从iOS Webview中的Angularjs应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆