简体   繁体   English

在 Angularjs 中使用 ng-init 初始化包含 ' 的值时出现错误

[英]I am getting error while initialize value containing ' using ng-init in Angularjs

I am getting an error while initialize value containing ' using ng-init in Angularjs.在 Angularjs 中使用 ng-init 初始化包含' 的值时出现错误。

for eg:例如:

@{
var textToDisplay="Suman's Question";
<div ng-app="" ng-init="myText='@textToDisplay'">
<h1>{{myText}}</h1>
</div>
}

The above query throwing error上面的查询抛出错误

[$parse:lexerr]

I am using MVC for rendering the view.我正在使用 MVC 来呈现视图。 Thanks in advance for the suggestion..预先感谢您的建议..

The Answer is as follows答案如下

@{
var textToDisplay="Suman's Question";
textToDisplay= textToDisplay.Replace("'", "\\'");
<div ng-app="" ng-init="myText='@textToDisplay'">
<h1>{{myText}}</h1>
</div>
}

And if it is using " then the code is like.如果它使用“,那么代码就像。

@{
var textToDisplay="Suman"s Question";
textToDisplay= textToDisplay.Replace("\"", "&#34");
<div ng-app="" ng-init="myText='@textToDisplay'">
<h1>{{myText}}</h1>
</div>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM