Javascript 角字符编码问题

Javascript 角字符编码问题,javascript,angularjs,encoding,Javascript,Angularjs,Encoding,我正在尝试显示从可以用html编码的数据库返回的值 what';起来 在ng repeat内部,我输出这个值,并将该值传递给一个函数进行处理,然后使用“trustAsHtml”函数返回 返回$sce.trustAsHtml(html) 当我第一次加载页面时,一切看起来都很棒。该值输出为what's up

我正在尝试显示从可以用html编码的数据库返回的值

what';起来

在ng repeat内部,我输出这个值,并将该值传递给一个函数进行处理,然后使用“trustAsHtml”函数返回

返回$sce.trustAsHtml(html)

当我第一次加载页面时,一切看起来都很棒。该值输出为
what's up

有什么想法吗?我被难住了。

你可以使用ngbindthlunsafe

<div ng-app ng-controller="MyCtrl">
    <ul>
    <li ng-repeat=" opt in opts" ng-bind-html-unsafe="getContent(opt)" >
        {{ opt.value }}
    </li>
    </ul>

    <p>{{opt}}</p>
</div>

function MyCtrl($scope) {
    $scope.getContent = function(obj){
        return obj.value + " " + obj.text
    }
    $scope.opts = [
        {value: 111, text: '<b>1st</b>' },
        {value: 222, text: '<i>2nd</i>' }
    ];
}

  • {{opt.value}}
{{opt}}

函数MyCtrl($scope){ $scope.getContent=函数(obj){ 返回obj.value+“”+obj.text } $scope.opts=[ {值:111,文本:'1st'}, {值:222,文本:'2nd'} ]; }

您可以使用ngBindHtmlUnsafe

<div ng-app ng-controller="MyCtrl">
    <ul>
    <li ng-repeat=" opt in opts" ng-bind-html-unsafe="getContent(opt)" >
        {{ opt.value }}
    </li>
    </ul>

    <p>{{opt}}</p>
</div>

function MyCtrl($scope) {
    $scope.getContent = function(obj){
        return obj.value + " " + obj.text
    }
    $scope.opts = [
        {value: 111, text: '<b>1st</b>' },
        {value: 222, text: '<i>2nd</i>' }
    ];
}

  • {{opt.value}}
{{opt}}

函数MyCtrl($scope){ $scope.getContent=函数(obj){ 返回obj.value+“”+obj.text } $scope.opts=[ {值:111,文本:'1st'}, {值:222,文本:'2nd'} ]; }

我不确定这一点,但我发现这可以帮助您。您可以创建一个指令,使用$watch函数更新$sce

<div ng-controller="myCtrl">
  <b>User comments</b><br>
  <div class="well">
    <div ng-repeat="userComment in myCtrl.userComments">
      <span ng-bind-html="userComment.htmlData"></span>
      <br>
    </div>
  </div>
</div>

var ngBindHtmlDirective = ['$sce', function($sce) {
  return function(scope, element, attr) {
    scope.$watch($sce.parseAsHtml(attr.ngBindHtml), function(value) {
      element.html(value || '');
    });
  };
}];

用户评论

var ngBindHtmlDirective=['$sce',函数($sce){ 返回函数(范围、元素、属性){ 作用域$watch($sce.parseAsHtml(attr.ngBindHtml),函数(值){ html(值| |“”); }); }; }];
我不确定这一点,但我发现这可以帮助您。您可以创建一个指令,使用$watch函数更新$sce

<div ng-controller="myCtrl">
  <b>User comments</b><br>
  <div class="well">
    <div ng-repeat="userComment in myCtrl.userComments">
      <span ng-bind-html="userComment.htmlData"></span>
      <br>
    </div>
  </div>
</div>

var ngBindHtmlDirective = ['$sce', function($sce) {
  return function(scope, element, attr) {
    scope.$watch($sce.parseAsHtml(attr.ngBindHtml), function(value) {
      element.html(value || '');
    });
  };
}];

用户评论

var ngBindHtmlDirective=['$sce',函数($sce){ 返回函数(范围、元素、属性){ 作用域$watch($sce.parseAsHtml(attr.ngBindHtml),函数(值){ html(值| |“”); }); }; }];