我在app.js中定义了不同的控制器和相应的模板URL:
var App = angular.module('FormApp', [ 'ngRoute','ui.bootstrap', 'dialogs', 'oc.modal' ]);
App.config([ '$routeProvider', function($routeProvider) {
$routeProvider.when('/addClient', {
templateUrl : '../../resources/partialHtml/addClientLayout.html',
controller : FormController
}).when('/conflist/:commandName/:client/:env', {
templateUrl : '../../resources/partialHtml/testPrase.html',
controller : TestParseController
}).when('/addNewCommand', {
templateUrl : '../../resources/partialHtml/addNewCommand.html',
controller : AddNewCommandController
})
} ]);
我的TestParseController是定义的:
var TestParseController = function($scope, $window, $http, $routeParams, $sce,
$compile) {
$scope.hide = function(obj) {
alert($routeParams.commandName);
};
$scope.to_trusted1 = function(html_code) {
html_code = $sce.trustAsHtml(html_code);
$scope.content = html_code;
alert(html_code);
$compile( document.getElementById('innerh'))($scope);
};
$http.get('client/getConfList/' + $routeParams.commandName)
.success(
function(data) {
$scope.html_content = "<button data-ng-click='hide($event)'>Click me!</button>";
$scope.to_trusted1($scope.html_content);
});
}
Html:testParse.html:
<h3 data-ng-click="hide($event)" class="plus">Add</h3>
<div ng-bind-html="content" id="innerh"> </div>
div已正确填充,但填充按钮的ng-click无效,但它适用于页面本身可用的h3标记。
有人请帮忙..
答案 0 :(得分:1)
使用以下内容更改 innerh div的填充方式,而不是$scope.content
(并从innerh div中移除ng-bind-html
):
document.getElementById('innerh').innerHTML = html_code;
进入to_trusted1
函数:
$scope.to_trusted1 = function(html_code) {
html_code = $sce.trustAsHtml(html_code);
$scope.content = html_code;
//alert(html_code);
document.getElementById('innerh').innerHTML = html_code;
};
jsfiddle :https://jsfiddle.net/m37xksxk/
解决方案2 :
更多AngularJS方式可能是使用$timeout
。
您可以使用它来确保内容包含在div中。你还必须得到div中的内容,因为这是将要编译的内容:angular.element(document.getElementById('innerh')).contents()
:
所以它会变成:
$timeout( function(){
$compile( angular.element(document.getElementById('innerh')).contents())
($scope);
}, 0);
jsfiddle 2 :https://jsfiddle.net/m37xksxk/1/
答案 1 :(得分:0)
您是否有理由不创建指令?你的实现非常混乱。这是我在jsfiddle中创建的东西:
app.directive('testContent', function () {
return {
template: "<button data-ng-click='hide($event)'>Click me!</button>"
};
});