This question already has an answer here:
这个问题已经有了答案:
- AngularJS ng-bind-html 2way data binding 2 answers
- AngularJS ng-bind-html 2way数据绑定2答案。
AngularJS v1.3.14: Currently, I'm successfully using the 'ngSanitize' module in my angular app with "ng-bind-html" to bind some HTML to the output of an area on the page. That works and doesn't require the older $sce 'trust HTML' stuff.
AngularJS v1.3.14:目前,我正在使用我的有棱角的应用“ng-bind-html”中的“ngSanitize”模块,将一些HTML绑定到页面上某个区域的输出。这是可行的,并且不需要旧的$sce“信任HTML”之类的东西。
But, I'd like to embed child bindings within that HTML.
但是,我想在HTML中嵌入子绑定。
So... something like this works...
所以…这样的作品……
angular.module('myApp', ['ngSanitize'...)
.controller('SomeCtrl', ['$scope', function($scope) {
$scope.myHTML = '<p>hello world</p>';
}]);
...
<div ng-app="myApp" ng-controller="SomeCtrl" ng-bind-html="myHTML"></div>
Something like this doesn't...
这样不…
angular.module('myApp', ['ngSanitize'...)
.controller('SomeCtrl', ['$scope', function($scope) {
$scope.myContent = 'hello world';
$scope.myHTML = '<p>{{myContent}}</p>';
}]);
...
<div ng-app="myApp" ng-controller="SomeCtrl" ng-bind-html="myHTML"></div>
Thoughts?
想法吗?
1 个解决方案
#1
2
A simple directive like this would do it:
像这样一个简单的指令就可以做到:
<div bindy="myHTML"></div>
.directive('bindy', function($compile) {
return {
link: function($scope, $element, $attrs) {
var html = $scope.$eval($attrs.bindy);
$element.html(html);
$compile($element.contents())($scope);
}
};
});
#1
2
A simple directive like this would do it:
像这样一个简单的指令就可以做到:
<div bindy="myHTML"></div>
.directive('bindy', function($compile) {
return {
link: function($scope, $element, $attrs) {
var html = $scope.$eval($attrs.bindy);
$element.html(html);
$compile($element.contents())($scope);
}
};
});