gpt4 book ai didi

javascript - ng-bind 工作正常,为什么 ng-bind-html 不工作

转载 作者:行者123 更新时间:2023-11-30 15:40:12 26 4
gpt4 key购买 nike

   angular.module('form', []).controller('formcontroller', ['$scope',
function($scope) {
$scope.input;
$scope.hello = "<h1> Welcome</h1>";
}
]);
<html>

<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

</head>

<body>
<form ng-app="form" ng-controller="formcontroller">
<span ng-bind="hello"></span>

<span ng-bind-html="hello"></span>
</form>

</body>

</html>

我试过用

它导致输出为

<h1> Welcome</h1>

我尝试通过替换 ng-bind-html 不唤醒并抛出错误。

<script>
angular.module('form', []).controller('formcontroller', ['$scope', function($scope) {

$scope.hello="<h1> Welcome</h1>";
}]);
</script>

Error: $sce:unsafe Require a safe/trusted value Attempting to use an unsafe value in a safe context.

请解释。

最佳答案

如果您包含 angular-sanitize脚本,通过将 HTML 解析为标记来清理输入

var miAp = angular.module('miAp', ['ngSanitize']);

miAp.controller('demoController', function($scope) {
$scope.bar = "<h1> Welcome</h1>";
});
<html>

<head>
<meta charset="utf-8">
<title>ngBind</title>

<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.9/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.1/angular-sanitize.min.js" type="text/javascript"></script>

<script src="cookies.js"></script>
</head>

<body ng-app="miAp" ng-controller="demoController">
<div ng-bind-html="bar"></div>
</body>

</html>

关于javascript - ng-bind 工作正常,为什么 ng-bind-html 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40929941/

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