myApp.directive('qsetAnswer', function(){
var linker = function(scope,element,attr) {
console.log(scope.body); // this prints fine.
};
return {
restrict:'A',
scope: '=info',
template: '<h5> ' + scope.body + ' </h5>', // this gives error.
link: linker
}
});
以上给出: - 范围未定义错误
修改: - template: '<h5>{{body}}</h5>',
的问题是我的{{body}}
已经包含html ie。 <p> abc </p>
像template: '{{body}}'
一样按照字符串打印<p> abc </p>
。
答案 0 :(得分:2)
由于您将HTML绑定到模板,因此可以使用ng-bind-html指令。
这假设您使用 Angular 1.2 +
template: '<h5 ng-html-bind="{{body}}"></h5>',
对于 Angular&lt; 1.2 ,请使用
template: '<h5 ng-html-bind-unsafe="{{body}}"></h5>',
如果遇到版本1.2+的$ sce问题,请阅读以下内容:
您可以告诉Angular信任您的HTML,如下所示:
$scope.ans = $sce.trustAsHtml(' hi <bold> p </bold>');