Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Ternary operator $parse error #8484

Closed
basarat opened this issue Aug 5, 2014 · 9 comments
Closed

Ternary operator $parse error #8484

basarat opened this issue Aug 5, 2014 · 9 comments

Comments

@basarat
Copy link
Contributor

basarat commented Aug 5, 2014

Simple demo:

<div ng-app="myApp">    
    <button ng-click="foo ? bar=true : bas=true">click me</button>
</div>
angular.module('myApp',[]);

Error : [$parse:syntax] Syntax Error: Token 'undefined' expected : at column null of the expression [foo ? bar=true : bas=true] starting at [foo ? bar=true : bas=true]

Fiddle : http://jsfiddle.net/basarat/pLMS6/

Angular Version : 1.2.21
I believe its feature not implemented.

@campersau
Copy link
Contributor

It works if you surround the assignments with brackets: http://jsfiddle.net/pLMS6/1/

@a8m
Copy link
Contributor

a8m commented Aug 6, 2014

It's just the precedence and associativity of operators.
so you can do something like that:

<button ng-click="foo ? (bar = true) : (bas = false)">Click</button>

@basarat
Copy link
Contributor Author

basarat commented Aug 6, 2014

It's just the precedence and Associativity of operators.

Yes but I think $parse has it wrong.

foo ? (bar = true) : (bas = false)

= (assignment) will be evaluated (by JavaScript) after ?: (conditional). So in JavaScript you don't need the (). Angular's $parse shouldn't need it either. Perhaps it should be documented if not fixed.

Ref : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence

@lgalfaso
Copy link
Contributor

lgalfaso commented Aug 6, 2014

Looks like a bug and one that was present since the introduction of the ternary operator

@caitp
Copy link
Contributor

caitp commented Aug 6, 2014

I feel like this is probably a WONTFIX, angular expressions aren't javascript, and can't be expected to be a bugfree javascript interpreter.

@petebacondarwin
Copy link
Member

But it could be quite a straightforward fix, if it is just about setting the precedence?

@petebacondarwin petebacondarwin added this to the 1.3.0 milestone Aug 6, 2014
@petebacondarwin
Copy link
Member

That being said we are trying to move away from assignment in expressions and a simple workaround is to call functions instead:

$scope.setBar = function(val) { $scope.bar = val; };
$scope.setBaz = function(val) { $scope.baz = val; };
foo ? setBar(true) : setBas(false)

will work

petebacondarwin added a commit to petebacondarwin/angular.js that referenced this issue Aug 6, 2014
@petebacondarwin petebacondarwin modified the milestones: 1.3.0, 1.3.0-beta.18 Aug 6, 2014
lgalfaso added a commit to lgalfaso/angular.js that referenced this issue Aug 6, 2014
Properly handle assignemnts inside ternary operators

Closes angular#8484
@lgalfaso
Copy link
Contributor

lgalfaso commented Aug 6, 2014

@petebacondarwin you beat me on this. The question is if within the ternary operator expressions should we allow assignments or a filter chain

@petebacondarwin
Copy link
Member

I quite like the idea of filterChain actually!

@lgalfaso lgalfaso removed their assignment Aug 7, 2014
rodyhaddad added a commit that referenced this issue Aug 11, 2014
Closes #8512
Closes #8484
CLoses #5434

Conflicts:
	test/ng/parseSpec.js
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.