javascript - AngularJS 中 ng-bind、ng-bind-html、ng-bind-html-unsafe 之间的区别?

标签 javascript angularjs ng-bind-html ng-bind

我想知道 ng-bind 之间的区别, ng-bind-htmlng-bind-html-unsafe

此外,当我运行下面的代码时,我收到如下错误:

代码:

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>HTML Injection Security in AngularJS</title>

<script type="text/javascript" src="/js/angular.js"></script>

<script type="text/javascript">

angular.module("myApp", []).controller("myController",function($scope)
{
    $scope.getValue = function()
    {
        return "<b>Hello World</b>";
    };
});

</script>

</head>
<body>

<div ng-app="myApp" ng-controller="myController">
    <span ng-bind-html="getValue();"></span>
    <span>Normal Text</span>
</div>

</body>
</html>

错误:

Error: [$sce:unsafe] Attempting to use an unsafe value in a safe context. http://errors.angularjs.org/1.3.11/$sce/unsafe at REGEX_STRING_REGEXP (https://www.angularapprj.com:4443/js/angular.js:63:12) at htmlSanitizer (https://www.angularapprj.com:4443/js/angular.js:15053:13) at getTrusted (https://www.angularapprj.com:4443/js/angular.js:15217:16) at Object.$get.sce.(anonymous function) [as getTrustedHtml] (https://www.angularapprj.com:4443/js/angular.js:15897:16) at Object.ngBindHtmlWatchAction [as fn] (https://www.angularapprj.com:4443/js/angular.js:20449:29) at Scope.$get.Scope.$digest (https://www.angularapprj.com:4443/js/angular.js:14230:29) at Scope.$get.Scope.$apply (https://www.angularapprj.com:4443/js/angular.js:14493:24) at bootstrapApply (https://www.angularapprj.com:4443/js/angular.js:1449:15) at Object.invoke (https://www.angularapprj.com:4443/js/angular.js:4182:17) at doBootstrap (https://www.angularapprj.com:4443/js/angular.js:1447:14)

这是什么意思?我知道在命令式代码中使用声明性代码并不是一个好的做法,尽管我只是尝试使用 <b>Hello World</b>ng-bind-html指令。

最佳答案

搜索后我从 https://docs.angularjs.org/guide/migration#ngbindhtmlunsafe-has-been-removed-and-replaced-by-ngbindhtml 得到了以下信息:在 Angular 1.3 版本中,他们已经从 ng-bind-html-unsafe 迁移到 ng-bind-html,尽管仍然存在一个问题,为什么我会收到帖子中提到的错误?

enter image description here

关于javascript - AngularJS 中 ng-bind、ng-bind-html、ng-bind-html-unsafe 之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28599203/

相关文章:

AngularJS 1.2.0 ngBindHtml 和 trustAsHtml 不适用于 ngModel

javascript - 无法在 AngularJS ng-views 中加载 Instagram 配置文件

javascript - 顶层对话框。改变背景颜色 : (EDIT) to Entire doc

javascript - 嵌入式youtube视频-仅播放没有音频的视频?

javascript - 如何选择一个按钮并将它的值显示到屏幕上

javascript - 使用 Ruby on Rails 上传静态网站并显示它们

javascript - 使用 ng-if 根据 ng-repeat 内的文件扩展名类型显示文本(Angular 的新功能)

javascript - 使用 AngularJS 在 Sharepoint 应用程序上构建快速启动菜单 - 如何缩进一些选项并禁用单击菜单标题

javascript - ngAttr 与 Angular 的条件属性

javascript - TypeScript + AngularJS : Can't interact with the html generated by ng-bind-html