AngularJS前pression在NG-点击抛出一个语法错误 [英] AngularJS expression throwing a Syntax error in ng-click
问题描述
好吧,我工作的一个网站,用户可以在原岗位或任何随后的后留下评论的注释部分。要做到这一点渲染的angular.js,我有一些有趣的递归。
Ok, I am working on a comment section of a website where users can leave comment on the original post or any subsequent post. To accomplish this rendering in angular.js, I had some fun with recursion.
<script type="text/ng-template" id="renderer.html">
{{comment.userName+' - '+comment.commentText}}
<br/><button class="standardButton">respond</button>
<ul>
<li ng-repeat="comment in comment.childComments | orderBy:'commentId'" ng-include="'renderer.html'"></li>
</ul>
</script>
<ul>
<li data-ng-repeat="comment in comments" data-ng-include="'renderer.html'"></li>
</ul>
这本身完美的作品。不过,我需要能够看到有关所单击按钮的数据。我更新了我的code这样:
This by itself works perfectly. However, I need to be able to see data about which button was clicked. I updated my code to this:
<script type="text/ng-template" id="renderer.html">
{{comment.userName+' - '+comment.commentText}}
<br/><button class="standardButton" ng-click='alertMe({{comment.userName}})'>respond</button>
<ul>
<li ng-repeat="comment in comment.childComments | orderBy:'commentId'" ng-include="'renderer.html'"></li>
</ul>
请注意添加了NG-点击。该alertMe方法与简单的字符串控制器成功进行了测试。它仅采用了棱角分明的前pressions时断裂。
Please notice the addition of the ng-click. The alertMe method was successfully tested in the controller with plain strings. It only breaks when using angular expressions.
我得到在控制台下面的错误加载的每一个意见:
I get the following error in the console for every comment loaded:
> Error: [$parse:syntax]
> http://errors.angularjs.org/1.2.15/$parse/syntax?p0=comment.userName&p1=is%20unexpected%2C%20expecting%20%5B%3A%5D&p2=11&p3=alertMe(%7B%7Bcomment.userName%7D%7D)&p4=comment.userName%7D%7D)
> at Error (native)
> at http://localhost:8080/Vail/jslib/angular.min.js:6:450
> at Za.throwError (http://localhost:8080/Vail/jslib/angular.min.js:162:211)
> at Za.consume (http://localhost:8080/Vail/jslib/angular.min.js:163:183)
> at Za.object (http://localhost:8080/Vail/jslib/angular.min.js:170:340)
> at Za.primary (http://localhost:8080/Vail/jslib/angular.min.js:161:358)
> at Za.unary (http://localhost:8080/Vail/jslib/angular.min.js:168:73)
> at Za.multiplicative (http://localhost:8080/Vail/jslib/angular.min.js:167:310)
> at Za.additive (http://localhost:8080/Vail/jslib/angular.min.js:167:170)
> at Za.relational (http://localhost:8080/Vail/jslib/angular.min.js:167:34) <button
> class="standardButton" ng-click="alertMe({{comment.userName}})">
角网站已经这样说的:
The angular site has this to say:
错误:$解析:语法语法错误语法错误:令牌
comment.userName是列{2}的前pression的[{3}]启动
在[{4}]。
Error: $parse:syntax Syntax Error Syntax Error: Token 'comment.userName' is at column {2} of the expression [{3}] starting at [{4}].
有谁知道是什么原因造成这个错误?任何及所有信息,大大AP preciated。
Does anybody know what is causing this error? Any and all information is greatly appreciated.
推荐答案
您不需要的 NG-点击
,因为它期待一个的前pression 的,不是一个模板。违规行更改为:
You don't need brackets in ng-click
, as it expects an expression, not a template. Change the offending line to:
<br/><button class="standardButton" ng-click='alertMe(comment.userName)'>respond</button>
这篇关于AngularJS前pression在NG-点击抛出一个语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!