Reputation: 245
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>
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>
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:
Error: $parse:syntax Syntax Error Syntax Error: Token 'comment.userName' is at column {2} of the expression [{3}] starting at [{4}].
Does anybody know what is causing this error? Any and all information is greatly appreciated.
Upvotes: 1
Views: 2043
Reputation: 39522
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>
Upvotes: 3