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

feat(ngSwitch): Allow multiple case matches for one element #3516

Closed
wants to merge 1 commit into from

Conversation

lrlopez
Copy link
Contributor

@lrlopez lrlopez commented Aug 9, 2013

Now you can also use an array in ng-switch-when, which allows specifying multiple values for the same element. Values will be evaluated within the scope when using the array notation.

I.e. ng-switch-when="[1, 3, 'hello', 5-3]"

Each element will switch only once even if there are multiple matches in the same element.

BREAKING CHANGE: Previously ng-switch-when expressions that matched against square brackets strings won't work anymore. The fix is encapsulate the string into a one-element array.
I.e. before ng-switch-when='[hi]', now ng-switch-when="['[hi]']"

Closes #3410

@mary-poppins
Copy link

Thanks for the PR!

  • Contributor signed CLA now or in the past
    • If you just signed, leave a comment here with your real name
  • PR's commit messages follow the commit message format

If you need to make changes to your pull request, you can update the commit with git commit --amend.
Then, update the pull request with git push -f.

Thanks again for your help!

@lrlopez
Copy link
Contributor Author

lrlopez commented Aug 9, 2013

This PR adds 60 bytes to the minified build.

Edit: Mary Poppins is now happy :)

@L42y
Copy link

L42y commented Aug 9, 2013

👍

@petebacondarwin
Copy link
Contributor

Arguably this has a breaking change in the case the someone wanted to match to a string that was wrapped in square brackets.

Now you can also use an array in `ng-switch-when`, which allows
specifying multiple values for the same element. Values will
be evaluated within the scope when using the array notation.

I.e. `ng-switch-when='[1, 3, 'hello', 5-3]'`

Each element will switch only once even if there are multiple
matches in the same element.

BREAKING CHANGE: Previously `ng-switch-when` expressions that
matched against square brackets strings won't work anymore.
The fix is encapsulate the string into a one-element array.

I.e. before `ng-switch-when='[hi]'`, now `ng-switch-when="['[hi]']"`

Closes angular#3410
@lrlopez
Copy link
Contributor Author

lrlopez commented Aug 10, 2013

@petebacondarwin Thanks, you're right. I've updated the commit message to address this.

@lgalfaso
Copy link
Contributor

Hi, even when I do see some value in this enhancement, the use of $eval gives a big potential for abusing of this feature. Why not limit this to JSON?

@lrlopez
Copy link
Contributor Author

lrlopez commented Aug 11, 2013

The idea behind using $eval is precisely that avoids abusing of interpolation: Instead of doing ng-switch-when="[{{var1}}, {{var2}}, {{var3}}]" you could do just ng-switch-when="[var1, var2, var3]"

Anyway, I could switch to JSON.parse if that is the general consensus.

@lgalfaso
Copy link
Contributor

If there is a general consensus that $eval is better, I am fine with that. Anyhow, doing an $eval has some unexpected side-effects

Say $scope.foo = $q.when('bar');, then $scope.$eval('[foo]'); will be an array with a promise and ['{{foo}}'] will print out ['bar'] that is what most people would expect

@lrlopez
Copy link
Contributor Author

lrlopez commented Aug 12, 2013

Either way I think it's a valuable feature. I hope it gets reviewed soon...

@lrlopez
Copy link
Contributor Author

lrlopez commented Jan 21, 2014

Could someone from the team tag this PR? Thank you!

@caitp
Copy link
Contributor

caitp commented Jan 28, 2014

@lrlopez backlog more or less means its on our list of things to get to, but it may not be a priority for this release (which is mostly revolving around migrating over to the new doc generator).

But, just to give it some quick review, the issue here is that it sort of says "hey, sometimes this attribute is a plain string, and sometimes it's treated as an angular expression", despite not actually using the expression parser.

I think we could have a better solution which doesn't add any inconsistency by having a separate attribute which is parsed as an expression, instead.

@lrlopez
Copy link
Contributor Author

lrlopez commented Jan 28, 2014

Caitlin, thanks for your reply. I found the backlog milestone description just after posting. I agree that a possible solution is having a separate attribute, does everybody agree? The new attribute could be named ng-switch-when-expr or something similar.

@atian25
Copy link

atian25 commented May 17, 2014

any good news for this issue?

@lrlopez
Copy link
Contributor Author

lrlopez commented May 18, 2014

@atian25, I'm just waiting for someone from the core team to give me the green light about the new attribute...

@rodyhaddad
Copy link
Contributor

Why not use a comma-separated list?
<div ng-switch-when="home, settings">

It's to easy to implement and understand

lgalfaso added a commit to lgalfaso/angular.js that referenced this pull request Jan 18, 2015
Adds an optional attribute `ngSwitchWhenSeparator` that allows multiple
tokens to match a given `ngSwitchWhen`.

Closes angular#3410
Closes angular#3516
lgalfaso added a commit to lgalfaso/angular.js that referenced this pull request Jan 18, 2015
Adds an optional attribute `ngSwitchWhenSeparator` that allows multiple
tokens to match a given `ngSwitchWhen`.

Closes angular#3410
Closes angular#3516
lgalfaso added a commit to lgalfaso/angular.js that referenced this pull request Jan 20, 2015
Adds an optional attribute `ngSwitchWhenSeparator` that allows multiple
tokens to match a given `ngSwitchWhen`.

Closes angular#3410
Closes angular#3516
@Narretz
Copy link
Contributor

Narretz commented Jun 9, 2015

#10798 is the way forward, I hope we can get it into 1.5

@Narretz Narretz closed this Jun 9, 2015
Narretz pushed a commit that referenced this pull request Sep 8, 2016
…SwitchWhenSeparator

Adds an optional attribute `ngSwitchWhenSeparator` that allows multiple tokens to match a given `ngSwitchWhen`.

Closes #3410
Closes #3516

Closes #10798
Narretz pushed a commit that referenced this pull request Sep 8, 2016
…SwitchWhenSeparator

Adds an optional attribute `ngSwitchWhenSeparator` that allows multiple tokens to match a given `ngSwitchWhen`.

Closes #3410
Closes #3516

Closes #10798
petebacondarwin pushed a commit to petebacondarwin/angular.js that referenced this pull request Nov 21, 2016
…SwitchWhenSeparator

Adds an optional attribute `ngSwitchWhenSeparator` that allows multiple tokens to match a given `ngSwitchWhen`.

Closes angular#3410
Closes angular#3516

Closes angular#10798
petebacondarwin pushed a commit to petebacondarwin/angular.js that referenced this pull request Nov 21, 2016
…SwitchWhenSeparator

Adds an optional attribute `ngSwitchWhenSeparator` that allows multiple tokens to match a given `ngSwitchWhen`.

Closes angular#3410
Closes angular#3516

Closes angular#10798
petebacondarwin pushed a commit to petebacondarwin/angular.js that referenced this pull request Nov 21, 2016
…SwitchWhenSeparator

Adds an optional attribute `ngSwitchWhenSeparator` that allows multiple tokens to match a given `ngSwitchWhen`.

Closes angular#3410
Closes angular#3516

Closes angular#10798
petebacondarwin pushed a commit to petebacondarwin/angular.js that referenced this pull request Nov 21, 2016
…SwitchWhenSeparator

Adds an optional attribute `ngSwitchWhenSeparator` that allows multiple tokens to match a given `ngSwitchWhen`.

Closes angular#3410
Closes angular#3516

Closes angular#10798
petebacondarwin pushed a commit to petebacondarwin/angular.js that referenced this pull request Nov 21, 2016
…SwitchWhenSeparator

Adds an optional attribute `ngSwitchWhenSeparator` that allows multiple tokens to match a given `ngSwitchWhen`.

Closes angular#3410
Closes angular#3516

Closes angular#10798
petebacondarwin pushed a commit that referenced this pull request Nov 23, 2016
…SwitchWhenSeparator

Adds an optional attribute `ngSwitchWhenSeparator` that allows multiple tokens to match a given `ngSwitchWhen`.

Closes #3410
Closes #3516

Closes #10798
petebacondarwin pushed a commit that referenced this pull request Nov 24, 2016
…SwitchWhenSeparator

Adds an optional attribute `ngSwitchWhenSeparator` that allows multiple tokens to match a given `ngSwitchWhen`.

Closes #3410
Closes #3516

Closes #10798
@vsraptor
Copy link

This does not seem to be working in angular 1.5.8 !!!

@gkalpak
Copy link
Member

gkalpak commented Dec 14, 2016

@vsraptor, we went with #10798 which will probably be included in 1.5.10.

@vsraptor
Copy link

When is the new version expected to be published

@gkalpak
Copy link
Member

gkalpak commented Dec 15, 2016

It should have been released by the end of next week (probably sooner).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

make ng-switch-when more powerful