如何在将 html 保存到数据库时对其进行转义
How to escape html while saving it to db?
我想在保存到数据库时转义特殊字符和html,我可以使用过滤器使用以下代码来完成该任务吗我收到错误您的模块未正确加载,我需要在app中添加依赖项吗.js。AngularJs 的新手任何帮助将不胜感激。
主.html
<textarea rows="2" class="form-control" id="name"
ng-model="processDTO.processLongName"
placeholder="Business Process Name" maxlength="1024" name="processName"
required
ng-bind-html="escapeHtml"
data-tooltip-html-unsafe="<div>{{1024 - processDTO.processLongName.length}} characters left</div>"
tooltip-trigger="{{{true: 'focus', false: 'never'}[processDTO.processLongName.length >= 0 || processDTO.processLongName.length == null ]}}"
tooltip-placement="top" tooltip-class="bluefill">
</textarea>
过滤器.js
angular
.module('riskAssessmentApp', [
'ngSanitize'
])
.filter('escapeHtml', function ($sce) {
// Modified by Rockallite: Add $sce.trustAsHtml() to mute "Error: $sce:unsafe"
// http://stackoverflow.com/a/32835368/2293304
// http://stackoverflow.com/a/28537958/2293304
// https://github.com/janl/mustache.js/blob/master/mustache.js#L82
var entityMap = {
"&": "&",
"<": "<",
">": ">",
'"': '"',
"'": ''',
"/": '/'
};
return function(str) {
return $sce.trustAsHtml(String(str).replace(/[&<>"''/]/g, function (s) {
return entityMap[s];
}));
}
});
应用.js
angular.module('riskAssessmentApp', [
'angularSpinner',
'ngResource',
'ui.router',
'ngCookies',
'bacMultiselect',
'kendo.directives',
'kendoMultiselectTreeview',
'offClick',
'myMaxlength',
'requireControlPoint',
'disableControlPoint',
'disablePageElements',
'progressStepbar',
'ui.bootstrap',
'orcit.ssoHandler',
'orcit.icon',
'orcit.multiselectTreeview',
'orcit.loader'
'ngSanitize'
]).config(function ($stateProvider, $httpProvider, $urlRouterProvider,$tooltipProvider) {
错误
[$injector:nomod] Module 'riskAssessmentApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
riskAssessmentApp
模块定义两次。
在你的filter.js
不要重新定义它,只需将过滤器附加到该模块:
angular.module('riskAssessmentApp')
.filter('escapeHtml', function ($sce) {
// Modified by Rockallite: Add $sce.trustAsHtml() to mute "Error: $sce:unsafe"
// http://stackoverflow.com/a/32835368/2293304
// http://stackoverflow.com/a/28537958/2293304
// https://github.com/janl/mustache.js/blob/master/mustache.js#L82
var entityMap = {
"&": "&",
"<": "<",
">": ">",
'"': '"',
"'": ''',
"/": '/'
};
return function(str) {
return $sce.trustAsHtml(String(str).replace(/[&<>"''/]/g, function (s) {
return entityMap[s];
}));
}
});
相关文章:
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 在JavaScript中输出转义字符
- 转义符不能与innerHTML一起使用
- 如何通过php页面将数据库值检索到jquery自动完成框中
- 使用javascript从数据库中添加表
- 通过CSV文件上载更新数据库表
- 平均值:无法将数据更新到数据库
- 如何使用 Angular JS 将数据保存在数据库中
- Rails 3.2 js.erb文件转义js
- Rails-JQueryUIAutcomplete和AJAX不工作,可以't连接到数据库
- 在谷歌地图上绘制位置数据库
- PHP:显示sqlite数据库中的html格式数据,使用tinymce保存
- 将地理编码结果转换为php变量以发布到mysql数据库
- 从数据库中检索字段,而不模拟它们
- javascript:将数据库中的数据插入到html5文本字段中
- 我想将长文本文档解析为SQLite数据库.如何转义所有不支持的字符
- 如何在将 html 保存到数据库时对其进行转义
- 如何转义 SQL 查询的单引号,用于在 cartodb 数据库中的 javascript/jquery 中生成的查询
- 文档.从mysql数据库编写javascript并处理转义
- 插入SQLite数据库时难以转义单引号字符