Angular.js: AngularJS expressions should support comma operator (,)

Created on 8 Oct 2014  路  9Comments  路  Source: angular/angular.js

It seems that one cannot use the comma operator (,) within Angular expressions.

I came around this while trying to extend a directive that disables an anchor tag (<a>) found on Stackoverflow http://stackoverflow.com/questions/23425254/enable-disable-anchor-tag-using-angularjs such that one can use "composite" ng-click expressions like in the following example (don't ask why not simply creating a new function in the scope that contains the two ng-click statements. Let's assume we can't; e.g., because we don't have control over the controller implementation)

<!-- The combined expression created by a-disabled doesn't parse because of the semicolon -->
<a href="/foo" a-disabled="blah" ng-click="fun(); funnier=false">

<!-- The combined expression created by a-disabled would parse if commas were supported,
and it would also do what we want (if `blah` is falsy, do not invoke `fun()` nor update `funnier`. -->
<a href="/foo" a-disabled="blah" ng-click="fun(), funnier=false">

Note that the ng-click expression using semicolons works fine without the a-disableddirective.

See also how the comma operator can be used in JavaScript (to do weird things):
http://stackoverflow.com/questions/9018295/javascript-tuple-notation-what-is-its-point

won't fix

All 9 comments

oh, please no

I don't think this has anything to do with the , operator. It's the parser that does not support ; separated expressions in (...) (I guess).

@twwwt :
In any case, this aDisabled directive looks really bad.
Here it is refactored a little bit, to do what you want (but it's ugly anyway).

@gkalpak
Thanks for the refactored version! I will have a look into it. Still, what makes this directive "ugly" in your opinion?

If there is a point in supporting commas in expressions apart from the probably not so convincing use case I have described, then I would appreciate to see this implemented some day. Otherwise, and for the rest of the "Anguers" that might come to the same question, could someone give reasons why commas should not be supported (security issues, difficult to ambiguous semantics, ...).

Although me and the comma operator are best friends, we decided that it's better to leave the comma operator out of angular core.

Honestly I think having more than one expression bound to ng-click is usually a poor choice; adding a fn to a controller is almost always better.

and that's probably why we should close the issue

don't put too much imperative logic in your views ^_^ you get terrible stacktraces from them (if any) if anything breaks, and it just ends up not being a very good thing to do

also remember to eat your vegetables

You can use the hacky way if you really need a comma:

(click)="[$event.preventDefault(), go()]"
Was this page helpful?
0 / 5 - 0 ratings