Filtro utf8_decode para Angular (arreglar tildes y Ñs)


Hola, para los que tengan problemas con las tildes o ñs cuando están consumiendo un webservice en angular les dejo un filtro que arregla los problemas de codificación.

app.filter('utf8_decode', function($sce) {
  return function(strData) {
    var tmpArr = []
    var i = 0
    var c1 = 0
    var seqlen = 0
    strData += ''
    while (i < strData.length) {
      c1 = strData.charCodeAt(i) & 0xFF
      seqlen = 0
      // http://en.wikipedia.org/wiki/UTF-8#Codepage_layout
      if (c1 <= 0xBF) {
        c1 = (c1 & 0x7F)
        seqlen = 1
      } else if (c1 <= 0xDF) {
        c1 = (c1 & 0x1F)
        seqlen = 2
      } else if (c1 <= 0xEF) {
        c1 = (c1 & 0x0F)
        seqlen = 3
      } else {
        c1 = (c1 & 0x07)
        seqlen = 4
      }
      for (var ai = 1; ai < seqlen; ++ai) {
        c1 = ((c1 << 0x06) | (strData.charCodeAt(ai + i) & 0x3F))
      }
      if (seqlen === 4) {
        c1 -= 0x10000
        tmpArr.push(String.fromCharCode(0xD800 | ((c1 >> 10) & 0x3FF)))
        tmpArr.push(String.fromCharCode(0xDC00 | (c1 & 0x3FF)))
      } else {
        tmpArr.push(String.fromCharCode(c1))
      }
      i += seqlen
    }
    return tmpArr.join('')
  };
});


Modo de uso:
{{item.description | utf8_decode }}

Fuente: http://locutus.io/php/xml/utf8_decode/

Lexo

No hay comentarios:

Publicar un comentario

Instagram