gpt4 book ai didi

javascript - $sce.trustAsHtml 不起作用?

转载 作者:行者123 更新时间:2023-12-03 02:12:38 24 4
gpt4 key购买 nike

我有一个警报如下:

$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>'));

我将其绑定(bind)到 html 页面:

 <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>

结果,实际输出:

    Error: Invalid Alias Name: Certain 
limitations apply to alias naming. '<a href="http://google.com/">Please
refer to the documentation</a>'

预期输出: 错误:别名无效:确定 限制适用于别名命名。 'Please refer to the documentation '

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

谢谢!

最佳答案

您的示例应该可以工作,请检查控制台中是否有任何错误。这是一个工作示例:

var app = angular.module('myApp', ['ngSanitize', 'ui.bootstrap']);
app.controller('myCtrl', function($scope, $sce) {
$scope.alerts = [];
$scope.addAlert = function(type, msg) {
$scope.alerts.push({
"type": type,
"msg": msg
});
}
$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>'));
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-sanitize/1.6.9/angular-sanitize.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.5.0/ui-bootstrap-tpls.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />

<div ng-app="myApp" ng-controller="myCtrl">

<div uib-alert ng-repeat="alert in alerts" ng-class="'alert alert-{{alert.type}}'">
<span ng-bind-html="alert.msg"></span>
</div>

</div>

注意:

  • 注入(inject)ngSanitize模块
  • 注入(inject)$sce
  • 使用<div uib-alert而不是<uib-alert (可选)
  • 改变typeng-class使用正确的 bootstrap CSS 语法

关于javascript - $sce.trustAsHtml 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49492130/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com