如何将html保存到db时将其转义?

时间:2016-02-22 15:07:19

标签: javascript angularjs html-escape-characters

我想在保存到数据库的同时转义特殊字符和html,我可以使用过滤器来实现下面代码的任务我收到错误你的模块没有正确加载,我是否需要在app.js中添加依赖项对AngularJs的新任何帮助将不胜感激。

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

filter.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 = {
        "&": "&amp;",
        "<": "&lt;",
        ">": "&gt;",
        '"': '&quot;',
        "'": '&#39;',
        "/": '&#x2F;'
    };

    return function(str) {
      return $sce.trustAsHtml(String(str).replace(/[&<>"'\/]/g, function (s) {
          return entityMap[s];
      }));
    }
  });

app.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) {

ERROR

[$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.

1 个答案:

答案 0 :(得分:2)

您定义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 = {
        "&": "&amp;",
        "<": "&lt;",
        ">": "&gt;",
        '"': '&quot;',
        "'": '&#39;',
        "/": '&#x2F;'
    };

    return function(str) {
      return $sce.trustAsHtml(String(str).replace(/[&<>"'\/]/g, function (s) {
          return entityMap[s];
      }));
    }
  });