ng模型中的特殊字符

时间:2016-03-03 23:44:56

标签: javascript html angularjs html5 special-characters

plunker

我有一个包含已解码的HTML实体的字符串:

'O'

ó

我需要通过string值在输入元素中显示此ng-model,但是用字符显示它,而不是解码实体。我已尝试将$parser$sce一起使用,但这似乎不起作用。

我尝试了一些不同的方法,但似乎没有任何工作

  .controller('myCtrl', function($scope, $sce){
    $scope.char = $sce.trustAsHtml('ó') 
  })
  .directive('trustedHtml', ['$sce', function($sce) {
    return {
      require: 'ngModel',
      link: function(scope, element, attrs, ngModel) {
        ngModel.$parsers.push(function(value) {
          console.log(value)
          return $sce.trustAsHtml(value);
        });
      }
    }
  }])

任何想法?

根据@Meiko的回答

working plunker

1 个答案:

答案 0 :(得分:1)

你必须htmlDecode字符串:

Feature

并使您的示例使用格式化程序而不是解析器:

 @Repository
 public class UserRepository {

    @PersistenceContext
    private EntityManager em;

    public void getMeta() {
        org.hibernate.engine.spi.SessionImplementor sessionImp = (org.hibernate.engine.spi.SessionImplementor) em.getDelegate();
        DatabaseMetaData metadata = null;
        try {
            metadata = sessionImp.connection().getMetaData();
            ResultSet res = metadata.getColumns(null, null, "USERS", "USERNAME");
            System.out.println("List of columns: ");
            while (res.next()) {
                System.out.println(
                        "  " + res.getString("TABLE_SCHEM")
                                + ", " + res.getString("TABLE_NAME")
                                + ", " + res.getString("COLUMN_NAME")
                                + ", " + res.getString("TYPE_NAME")
                                + ", " + res.getInt("COLUMN_SIZE")
                                + ", " + res.getInt("NULLABLE"));
            }
            res.close();
            System.out.println();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

    function htmlDecode(input){
      var elem = document.createElement('div');
      elem.innerHTML = input;
      return elem.childNodes.length === 0 ? '' : elem.childNodes[0].nodeValue;
    }
ngModel.$formatters.push(function(value) {
    return htmlDecode(value);
});