Angularjs指令: - 范围未定义错误

时间:2013-11-19 17:45:01

标签: angularjs angularjs-directive

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>

1 个答案:

答案 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>');
相关问题