如何使用ng-init在angularjs中为quote赋值

时间:2014-10-13 10:52:01

标签: angularjs variables ng-init

<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"'因为值来自数据库,可能包含'"符号。)

1 个答案:

答案 0 :(得分: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>