首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >$sce.trustAsHtml不工作了吗?

$sce.trustAsHtml不工作了吗?
EN

Stack Overflow用户
提问于 2018-03-26 13:01:51
回答 1查看 2K关注 0票数 0

我有一个警告如下:

代码语言:javascript
复制
$scope.addAlert('danger', $sce.trustAsHtml('Invalid Alias Name: Certain 
limitations apply to alias naming. <a href="http://google.com/">Please refer 
to the documentation</a>'));

我将其绑定到html页面如下:

代码语言:javascript
复制
 <uib-alert ng-repeat="alert in alerts" type="{{alert.type}}" 
 close="closeAlert($index, alerts)" dismiss-on-timeout="2500">
            <span ng-bind-html="alert.msg"></span>
        </uib-alert>

因此,实际输出:

代码语言:javascript
复制
    Error: Invalid Alias Name: Certain 
    limitations apply to alias naming. '<a href="http://google.com/">Please 
    refer to the documentation</a>'

预期输出:错误:别名名称无效:某些限制适用于别名命名。'请参阅文件

有人能帮忙吗?我不知道我错过了什么!

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2018-03-26 13:33:48

您是否可以尝试下面的代码,也可以为给定的示例场景检查这个工作的柱塞

模板:

代码语言:javascript
复制
<body ng-controller="MainCtrl">
    <div ng-repeat="alert in alerts">
      <h4 ng-bind="alert.title"></h4>
      <span ng-bind-html="alert.msg | formatContent"></span>
    </div>
</body>

控制器:

代码语言:javascript
复制
app.controller('MainCtrl', function($scope, $sce) {
  $scope.alerts=[];
  $scope.addAlert=function(title, msg){
    $scope.alerts.push({title: title, msg: msg});
  }
  $scope.addAlert('danger', 'Invalid Alias Name: Certain limitations apply to alias naming. <a href="http://google.com/">Please referto the documentation</a>');
});

app.filter('formatContent', ['$sce', function($sce){
  return function(input){
    return $sce.trustAsHtml(input);
  }
}]);

我已经更新了我的答案,用过滤器实现它,如上面的代码所示,还检查了正在使用过滤器实现的这个柱塞连杆

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49492130

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档