我在这里包含了一个Plunker:http://plnkr.co/edit/4vqV8toHo0vNjtfICtzI?p = preview
我正在尝试向DOM添加一个按钮,当单击时应该执行绑定到它的函数.在这种情况下,它应警告"测试".这是代码.
调节器
app.controller('MainCtrl', function($scope, $sce) {
$scope.trustedHtml = $sce.trustAsHtml('<button ng-click="testAlert()">Submit</button>');
$scope.testAlert = function () {
alert('testing')
};
});
Run Code Online (Sandbox Code Playgroud)
HTML
<body ng-controller="MainCtrl">
<div ng-bind-html="trustedHtml"></div>
</body>
Run Code Online (Sandbox Code Playgroud) 我已经创建了三个插件来说明我的问题.我正在尝试创建一个AngularJS指令,它将初始化基础并将必要的javascript应用于加载的模板.起初我尝试使用ngInclude将Foundation 5导航栏添加到我网站的所有页面.当html直接应用于部分时,顶部栏按预期工作.当html被添加到指令中时,例如ngInclude,顶部栏会丢失其所有功能.我怀疑这是因为在指令添加模板后基础没有被初始化.作为一个解决方案,我创建了一个自定义指令,用于初始化基础并编译html模板.以我冻结应用程序的方式初始化基础.有人有解决方案吗?
试图在不诉诸Angular UI的情况下实现这一目标.
示例1:直接应用于视图的HTML.按预期工作,当您单击菜单下拉列表时,将显示页面.
http://plnkr.co/edit/aQc6j2W9MpRuJo822gAF?p=preview
示例2:用于将模板加载到dom的ngInclude.没有实现任何功能,当您单击菜单下拉列表时没有任何反应.
http://plnkr.co/edit/fSS3FfYKFilMXsIkYUHg?p=preview
示例3:创建单独的指令以替换将初始化基础,编译并将模板加载到DOM的ngInclude.不能提供一个plunkr因为它会冻结,但这里是代码.
.directive('testdirective', function($compile) {
return {
restrict: 'AE',
templateUrl: 'partials/includes/nav.html',
link: function(scope, element, attrs) {
$compile($(document).foundation())(scope);
}
}
})
Run Code Online (Sandbox Code Playgroud)
部分应用:
<div testdirective></div>
Run Code Online (Sandbox Code Playgroud) 我的问题是当我尝试从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()"];
}
Run Code Online (Sandbox Code Playgroud)
ANGULARJS控制器:
'use strict';
angular.module('testing.controllers', [])
.controller('Testing', function($scope) {
function testing() {
alert('testing');
}
});
Run Code Online (Sandbox Code Playgroud)
基本HTML(与当前目标-C一起使用):
<!DOCTYPE HTML>
<html>
<body>
<SCRIPT>
function testing() {
alert('testing');
}
</SCRIPT>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 在运行应用程序之前,我收到错误消息"点和整数之间的比较('int'和'void*')使用NULL".奇怪的是应用程序每次运行都很完美.这是代码的片段.
- (IBAction) addButtonAction:(id)sender
{
int timeStamp = nil;
timeStamp = self.hourTextField.intValue;
if (timeStamp == NULL) {
NSLog(@"Nothing here for ya");
}
else {
NSLog(@"monkey shit");
}
}
Run Code Online (Sandbox Code Playgroud)
我在线上得到了错误.
if (timeStamp == NULL) {
Run Code Online (Sandbox Code Playgroud)