【问题标题】:How to make Angular 2 support ÆØÅ characthers?如何让 Angular 2 支持 ÆØÅ 字符?
【发布时间】:2017-08-08 05:07:24
【问题描述】:

我试图列出一个包含 ÆØÅ 字符的单词的数组。我可以使用 for 循环列出数组,并且数组中的单词会出来,但无法显示 ÆØÅ 字符 - 而是显示一个问号。数组的类型为string[]

我尝试在我的 index.html 文件的 head 部分添加一个带有 charset="utf-8" 的元标记,但这似乎有帮助。

还有什么我可以尝试让 ÆØÅ 与 Angular 2 一起工作的,通过将包含 æøå 的单词添加到数组中应该没有任何问题?

我循环遍历数组的 HTML 文件

<div class="col-md-6 col-sm-6" *ngFor="let heatingType of heatingTypes; let i=index">
            <div class="col-md-1 col-sm-1">
                <input type="checkbox" value="{{ i }}" class="form-control" 
                       formControlName="heating" [checked]="heating.indexOf(heatingType) > -1" 
                       (click)="toggleSelection(heatingType)">
            </div>
            <div class="col-md-11 col-sm-11">
                <label for="heating">{{ heatingType }}</label>
            </div>
        </div>

index.html

<!DOCTYPE html>
<html>
<head>
    <base href="/" />
    <title>Some app</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- polyfill(s) for older browsers -->
    <script src="node_modules/core-js/client/shim.min.js"></script>
    <script src="node_modules/zone.js/dist/zone.js"></script>
    <script src="node_modules/reflect-metadata/Reflect.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <script src="systemjs.config.js"></script>
    <script>
        System.import('src/app').catch(function (err) { console.error(err); });
    </script>
</head>
<body>
    <app>
        <span class="apploader">
            Laster inn applikasjonen<br />
            Vennligst vent ...
        </span>
    </app>
    </body>
</html>

数组

heatingTypes: string[] = [
    'Elektriske ovner', 'Varmepumpe', 'Vannbåren varme',
    'Varmekabler', 'Sentralfyr/Fjernvarme', 'Vedfyring', 'Annet'
];

Checkboxes

【问题讨论】:

  • 快速测试对我来说似乎工作正常。您能否发布一些代码,以便我们更好地了解您正在使用的内容?
  • 当然,我已经更新了问题描述。
  • 这些字符直接加到HTML里能正常显示吗?确保您的网站没有在标题中输出不同的字符集。
  • 是的,æøå 字符通常以 HTML 格式显示。我只是将相同的代码 - 带有 for 循环和数组的 html 内容 - 复制到另一个组件,然后它正确地显示了单词.. 这很奇怪,对吧?

标签: javascript arrays angular character-encoding


【解决方案1】:

如果您已经设置了元标记,请确保 HTML 文件也以 UTF-8 格式保存,一些编辑器以不同的编码保存文件(例如 Mac OS X 的 Sublime)。要验证请执行以下操作

  1. 在记事本中打开文件
  2. 单击另存为并查看在文件类型下方选择的编码。如果是 ASCII 或其他格式,请将其更改为 UTF-8 并保存文件并重新加载,它应该可以正常工作。

【讨论】:

  • 好吧,ive tried that now and it still dosent work. I just deleted the file, and added the entire code to Notepad - to clean the formatting - , then created a new file (with the same content) with utf-8 as encoding, and copied the code from notepad to the newly created html file. But the problem remains... Im 使用 Visual Studio 2017 顺便说一句。
  • 在将更改推送到 TFS 后,您是否在本地或服务器中遇到此问题?如果您已将更改推送到 TFS 并遇到从 TFS 推送到服务器的文件问题,请转到源代码管理并选择文件 -> 右键单击​​ -> 单击高级并检查属性中设置的编码。
  • 所以,它现在可以工作了。我从数组中删除了“Vannbåren varme”这个词并重写了它。似乎该当前单词的编码尚未更新。不管怎么说,还是要谢谢你!下次遇到同样的问题时,我会尝试这个解决方案。
猜你喜欢
  • 2017-11-21
  • 1970-01-01
  • 1970-01-01
  • 2013-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多