【问题标题】:AngularJS, special character from Json via serverAngularJS,来自 Json 通过服务器的特殊字符
【发布时间】:2021-07-26 13:32:40
【问题描述】:

我正在尝试使用 NodeJS 版本 10.15.1、AngularJS 版本 1.5.8 和 UTF8 编码的 html 创建一个小型多语言项目。我应该继续我自己的功能,而不是使用其他模块。
我创建了 2 个不同的 json 文件,其中包含 2 种不同的语言。 json 通过服务器使用$http 调用加载,答案存储在$scope 变量中。

$http.post(apihost + '/languages/language_frontend', {page: "home"}).then(function(language) {
   $scope.language = language.json;
});

我通过参数page 来过滤函数应该检索的部分json。

router.post('/language_frontend', function(req, res, next) {
   return new Promise(function(resolve,reject) {
      if(config.language == 'it') return res.json({status: 'ok', json: italian_frontend[req.body.page]});
      else if(config.language == 'en') return res.json({status: 'ok', json: english_frontend[req.body.page]});
   });
});

这是其中一个 json 的(部分)

{
   "home": {
      "planning": "Pianificazione",
      "activities_planning": "Pianificazione Attività"
   },
   "login": {
      "test_one": "italiano uno",
      "test_one": "italiano due"
   }
}

这是显示信息的html

<div class="panel-heading">
   <div class="row">
      <div class="col-xs-3"><i class="fa fa-mobile-phone fa-5x"></i></div>
      <div class="col-xs-9 text-right">
         <div class="huge ng-binding">{{language.activities_planning}}</div>
      </div>
   </div>
</div>

问题是activities_planning 的显示带有重音字符,并且来自服务器端调用,我不知道如何正确显示它。我想要一个在任何地方都可以实现的通用解决方案,所以我不必担心特殊字符的少数例外情况。

这是没有解决方案的结果:Pianificazione Attivit�

有什么建议吗?

【问题讨论】:

  • 我试图重现相同的代码,但我对特殊字符没有任何问题。也许问题出在代码中的其他地方。如果你愿意,我可以上传我的工作版本。
  • @ĐăngKhoaĐinh 是的,请......我仍然无法弄清楚,任何帮助将不胜感激,谢谢!

标签: node.js json angularjs special-characters


【解决方案1】:

所以,这里是https://glitch.com/edit/#!/angularjs-specialchars。我试着和你做同样的事情:

在我的后端app.js 中,我获取JSON 文件的内容并将其暴露在/language 路由中:

const path = require("path");
const express = require("express");
const app = express();
const language = require("./test.json");

app.use('/', express.static(path.join(__dirname, 'public')));
app.get('/language', (req, res) => res.json({ status: "ok", json: language }));

app.listen(5000, function() {console.log("Server is running on port 5000")});

在客户端的index.js 中,我向服务器发送请求以获取 JSON 文件:

angular.module("app", []).controller("MyController", ["$scope", "$http",
    function ($scope, $http) {

        // send request to get the json 
        $http.get('/language').then(function (resp) {
            const language = resp.data.json;
            console.log(language); // I've checked on the console.log, the text is OK
            $scope.text = language.test; // bind to screen
        });

    }
]);

在我的index.html 中,我只是使用它:

<body ng-app="app">
    <div ng-controller="MyController">
        <h1>Hello {{text}}!</h1>
    </div>
</body>

我有什么:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    相关资源
    最近更新 更多