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/