Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bo-switch-when does not work, when value is a string #87

Open
tylkomat opened this issue May 16, 2014 · 7 comments
Open

bo-switch-when does not work, when value is a string #87

tylkomat opened this issue May 16, 2014 · 7 comments
Labels

Comments

@tylkomat
Copy link

Bo-switch-when does not select the correct value, when comparing with a string. Here is an example:

<div bo-switch="value">
  <div bo-switch-when="'success'">It worked</div>
  <div bo-switch-default>It didn't work</div>
</div>

http://plnkr.co/edit/FUrCzW9sTuLtYOAFgCqc?p=preview

@tylkomat
Copy link
Author

My quick solution just now was using $eval in line 151 and then it works, but just parsing the value should be enough I suppose, scope might not be needed.

case 'boSwitchWhen':
  var ctrl = binder.controller[0]; console.log(binder.attrs.boSwitchWhen);
  ctrl.cases['!' + binder.scope.$eval(binder.attrs.boSwitchWhen)] = 
    (ctrl.cases['!' + binder.scope.$eval(binder.attrs.boSwitchWhen)] || []);
  ctrl.cases['!' + binder.scope.$eval(binder.attrs.boSwitchWhen)]
    .push({ transclude: binder.transclude, element: binder.element });
  break;

@Pasvaz
Copy link
Owner

Pasvaz commented May 16, 2014

Hi @tylkomat
Actually bo-switch, as well as ng-switch, use string for comparison, indeed if you remove the quotes everything works as expected:

<div bo-switch="value">
  <div bo-switch-when="success">It worked</div>
  <div bo-switch-default>It didn't work</div>
</div>

@Pasvaz Pasvaz closed this as completed May 16, 2014
@tylkomat
Copy link
Author

Indeed, but not if that string is a url. My solution has also the advantage, that you can compare to another scope value. I think it also makes sense to use expression here as it is also used everywhere else.

BTW the example in the readme also uses quotes, at least one ^^

@Pasvaz
Copy link
Owner

Pasvaz commented May 17, 2014

I know what you mean but I'm trying to keep the bo-* directives as close as possible to the ng-* directive so people can use them as drop-in replacement for angular standard directives. The change you suggest would break the compatibility with ng-switch.

Thank you for noticing the readme, I'll change it straight away.

@tylkomat
Copy link
Author

Ok I understand, but still it does not work, when the String you compare to is a URL. See this example: http://plnkr.co/edit/p7yuQivWk5RuLhwPRw3e?p=preview
Plunker does not show the JS error.

@Pasvaz
Copy link
Owner

Pasvaz commented May 17, 2014

This is indeed a bug, scope.$eval cannot parse special characters like : or /
It's not related to bo-switch, it is triggered by the default evaluation of the attribute, I'll fix it asap, I'm going to skip the $eval for those tag that doesn't need it.
As temporary workaround you can use:

 <div bo-switch-when="'http://test.com'">It worked</div>

and in the controller:

 $scope.value = "'http://test.com'";

@Pasvaz Pasvaz reopened this May 17, 2014
@Pasvaz Pasvaz added the bug label May 17, 2014
@tylkomat
Copy link
Author

Great, Thanks.

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

No branches or pull requests

2 participants