1
<script>
var a="what&#39;s";
console.log(a);//outputs what's;
</script>

但是当我尝试在 ng-init 中分配该值时

<div ng-init="a='what&#39;s'">

Angular 抛出错误,因为它与

ng-init="a='what's'"

那么我应该怎么做才能解决这个问题呢?
(注意:我不能在这里做,ng-init='a="what's"'因为该值来自可能同时包含'"符号的数据库。)

4

1 回答 1

2
<div ng-init="a='what\'s'">{{a}}</div>

var app = angular.module('app',[]);

app.controller('fCtrl', function($scope){
  
  
  
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
    <div ng-controller="fCtrl">
      <div ng-init="a='what\'s'">{{a}}</div>
    </div>
    </div>

于 2014-10-13T11:08:34.103 回答