您需要導入ngSanitize
模塊並使用$sce
服務。它應該是這個樣子:
// Remember the following comes from angular-sanitize.js found on the angular website and
// also must be included in the web app.
angular.module('myApp', ['ngSanitize']);
angular.module('myApp')
.controller('MyCtrl', function($scope, $sce) {
//... other controller code
$scope.renderHtml = function(html) {
return $sce.trustAsHtml(html);
};
});
總之,$sce
服務將迎來HTML作爲值得信賴的。你可以找到文件here
編輯:我意識到我可能沒有回答這個問題。看起來你問的是綁定作用域變量到你的指令中呈現的指令嗎?爲了讓元素正確編譯,你將不得不使用$compile
服務並改變你的邏輯。首先,模板:
<p class="place-directive-here"></p>
然後指令:
angular.module('myApp')
.directive('myDirective', function($compile) {
return {
scope: {
inlineWidget: '='
},
template: '<p class="place-directive-here"></p>',
link: function(scope, elem, attrs) {
var placement = elem.find('.place-directive-here');
scope.$watch('inlineWidget', function(widget){
if(!widget){
return;
}
// Compile the html against the scope. This will bind the dom to your
// current scope
var newElement = $compile(widget.blah)(scope);
// Place in the view
placement.html(newElement);
});
}
};
});
您可以找到編譯文檔here。希望這是一個更全面的答案,你在找什麼。
編輯2:爲了澄清,指令應該是這樣的網頁上:
<div my-directive inline-widget="someInlineWidget"></div>
的place-directive-here
類只是一個用於指令找出你<p>
標籤和渲染它的內側把手。但是,如果角度不與被的my-directive
內呈現的HTML而言,我們提供的第一個解決方案應該只是罰款和my-directive
模板屬性應該是:
template: '<p ng-bind-html="renderHtml(inlineWidget.blah)"></p>'
您是否包含['ngSanitize'](https://docs.angularjs.org/api/ngSanitize)模塊? renderHTml函數在哪裏? – PSL 2014-10-27 18:18:13