javascript 角度:错误:[$parse:syntax] 语法错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32274026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular: Error: [$parse:syntax] Syntax Error
提问by luklapp
I want to create tooltips in an ng-repeat
with angular-tooltips. The content of the tooltips has to be dynamically. What I already have is something like this:
我想在创建工具提示ng-repeat
与角提示。工具提示的内容必须是动态的。我已经拥有的是这样的:
View:
看法:
<div ng-repeat="region in regions">
<a tooltips tooltip-html="{{ myCtrl.generateTooltip(region) }}">HOVER</a>
</div>
Controller:
控制器:
function generateTooltip(region) {
// generate some html here
var content = "<b>HELLO WORLD!</b>";
return $sce.trustAsHtml( content );
}
The tooltip is shown and working, but I get the following error in console (which I don't want to see :-))
工具提示已显示并正常工作,但我在控制台中收到以下错误(我不想看到 :-))
Error: [$parse:syntax] Syntax Error: Token '<' not a primary
expression at column 1 of the expression [<b>HELLO WORLD!</b>]
starting at [<b>HELLO WORLD!</b>].
- What is wrong with my code?
- Is it possible to use a view as the tooltip instead of generating the HTML in the controller? There is an attribute
tooltip-view
, but I don't know how to pass myregion
variable to it.
- 我的代码有什么问题?
- 是否可以使用视图作为工具提示而不是在控制器中生成 HTML?有一个属性
tooltip-view
,但我不知道如何将我的region
变量传递给它。
回答by Justin Schulz
Someone else had a similar error resulting from a function running after the template was compiled by angular. Maybe this will set you in the right direction.
其他人因在 angular 编译模板后运行的函数而导致类似错误。也许这会让你朝着正确的方向前进。
回答by Ridham Tarpara
You need to include angular-sanitize.js
which sanitize inputs by parsing the HTML into tokens. All safe tokens (from a whitelist) are then serialized back to properly escaped HTML string. This means that no unsafe input can make it into the returned string.
您需要angular-sanitize.js
通过将 HTML 解析为标记来包含哪些清理输入。然后将所有安全令牌(来自白名单)序列化回正确转义的 HTML 字符串。这意味着没有不安全的输入可以使其进入返回的字符串。