The problem arises because it $sce.trustAsHtmldoes not return an HTML string, and jQuery overrides the method .html.
You can fix the problem:
var app = angular.module('plunker', []);
app.directive('dynamic', function ($compile) {
return {
restrict: 'A',
replace: true,
link: function (scope, ele, attrs) {
scope.$watch(attrs.dynamic, function (html) {
ele.html(html.$$unwrapTrustedValue());
$compile(ele.contents())(scope);
});
}
};
})
app.controller('MainCtrl', function ($scope, $sce, $compile) {
$scope.trustedHtml = $sce.trustAsHtml('<button ng-click="testAlert()">Submit</button>');
$scope.testAlert = function () {
alert('testing');
};
});
Plunkr
NOTE . This solves the problem, but I do not consider the use of $$unwrapTrustedValuegood practice. A better solution would be to have a template that binds to attrs.dynamic.
This is the best solution: http://plnkr.co/edit/xjS9gTJfyXvTL4LNzXub?p=preview
var app = angular.module('plunker', []);
app.directive('dynamic', function ($compile) {
return {
restrict: 'A',
replace: true,
template: '<span ng-bind-html="dynamic" ng-click="method()"></span>',
scope: {
dynamic: '=',
method: '&'
}
};
})
app.controller('MainCtrl', function ($scope, $sce, $compile) {
$scope.trustedHtml = $sce.trustAsHtml('<button>Submit</button>');
$scope.testAlert = function () {
alert('testing');
};
});
HTML
<div dynamic="trustedHtml" method="testAlert()"></div>
source
share