Rendering unicode-utf-8 turkish characters with an

2019-05-23 14:26发布

问题:

Characters from a binded JSON text, Turkish the letters are shown with wrong encoding for eg. &Ouml;zlem G&uuml;zelharcan which shall look like "özlem güzelharcan". I added <meta characters="utf-8"> in the head still no solution and there was no problem with laravel blade views.

If necessary this is how I get and use data:

view:

<div class="comment" ng-hide="loading" ng-repeat="comment in comments">
Comment #{{ comment.id }} </h3> <p>{{comment.title}}</p>
{{comment.author_id}} / {{comment.author.name}}

Services:

        // public/js/services/commentService.js
    angular.module('commentService', [])

        .factory('Comment', function($http) {

            var data =  {
                // get all the comments
                get : function() {
                    return $http.get('/api/comments/');
                }
            }
            console.log(data);
            return data;
        });

//controller (shortly)
.controller('mainController', function($scope, $http, Comment) {
        // object to hold all the data for the new comment form
        $scope.commentData = {};

        // loading variable to show the spinning loading icon
        $scope.loading = true;

        // get all the comments first and bind it to the $scope.comments object
        // use the function we created in our service
        // GET ALL COMMENTS ====================================================
        Comment.get()
            .success(function(data) {
                $scope.comments = data;
                $scope.loading = false;
            });

    });

Which method is used to clean characters with AngularJS? Thanks

回答1:

Eventually, after trying many things, I discovered that you have to use ng-bind-html or ng-bind-html-unsafe (with ngSanitize) to get the correct encoding. Here is how it works in my view:

Comment #<span ng-bind-template="{{comment.id}}"></span> </h3> 
        <span ng-bind-html="comment.title "></span>
        <p><div ng-bind-html="comment.content | truncate:25"></div></p>