当前位置: 移动技术网 > IT编程>脚本编程>AngularJs > angular4 获取wifi列表中文显示乱码问题的解决

angular4 获取wifi列表中文显示乱码问题的解决

2018年11月03日  | 移动技术网IT编程  | 我要评论

问题描述:

如果搜索到的wifi是中文名称,那么就会显示特殊字符比如(甿ªç”Ÿè迪),乱码的问题。

这里需要使用 escape方法对特殊字符编码,然后使用 decodeuricomponent解码

具体代码如下

创建一个管道,在需要的地方使用即可。

import { pipe, pipetransform, injectable } from "@angular/core";
// escape在管道中需要定义一下,要不然编译报错。
declare function escape(s: string): string;
@pipe({
 name: "ascpipe"
})
@injectable()
export class asciitogbkpipe implements pipetransform {
 transform(value: any) {
 console.log(value);
 if (value) {
  let str = escape(value);
  let dec = decodeuricomponent(str);
  return dec;
 } else {
  return value;
 }
 }
}

补充:angular2/angular4地址栏中文乱码

有时候我们需要在url传递中文参数,但是获取时会出现乱码。网上找了好多方法,但是从前端解析的话能够真正生效的还没有找到。病急乱投医,竟然被我试出来了。话不多说,方法就是用encodeuricomponent转义两次。即:encodeuricomponent(encodeuricomponent(你要转换的对象))。

 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网