简体   繁体   中英

Comments in angularjs expression

I have some HTML tags which have ng-clicks and ng-ifs. Inside the respective expressions, I make function calls and pass in parameters, some of which are literals (mostly just true or false). So I would like to add a comment as to what a literal means, but angular doesn't seem to be able to parse this correctly. (I realize passing literals is not the brightest idea but I would nevertheless like to know the answer)

<button class='someclass' ng-click='somefunction(val1, val2, true /* explanation for literal */)' > </button>

How do I add comments in angular expressions?

No, comments are not supported. Parser sees / as an mathematical operator (see source code ) which expects primary expression after it: eg something starting with ( , or [ , etc. However there is no valid expression in javascript that can include * immediately after / character. So parser throws an exception: Token '*' not a primary expression .

While the Angular documentation doesn't explicitly say that JavaScript comments are not supported. I would assume they are not.

Angular Expressions are only a subset of some JavaScript (and some added features like filters).

Why can't you pass these comments as separate parameters instead of appending other parameters? Even though you achieve doing this somehow it would be not very good design.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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