2

I am trying to use a switch to control some image url parameters. My image.marketplace node is a image.url.indexOf('?') and evaluates to either -1 or a positive integer if a question mark is present in the url. I can get my expression (ng-switch on="{image.marketplace == -1}") to evaluate properly, but it does not trigger the correct "when" condition. Any thoughts about why?

<span ng-switch on="{image.marketplace == -1}">
  <img ng-switch-when="true"  ng-src="{{image.value}}?wid=100&hei=100">
  <img ng-switch-when="false" ng-src="{{image.value}}&wid=100&hei=100">
  <img ng-switch-default      ng-src="{{image.value}}">
</span>
jdstein1
  • 117
  • 2
  • 10

1 Answers1

3

The ngSwitchOn directive doesn't require curly brackets, as shown by the documentation. It's probably a better idea to make something like that :

<span ng-switch on="isMarketplaced(image)">
  <img ng-switch-when="true"  ng-src="{{image.value}}?wid=100&hei=100">
  <img ng-switch-when="false" ng-src="{{image.value}}&wid=100&hei=100">
  <img ng-switch-default      ng-src="{{image.value}}">
</span>

And in the controller :

$scope.isMarketplaced = function (image)
{
    return image.marketplace == -1;
}

In addition, it will help you to make unit tests on this function.

Blackhole
  • 20,129
  • 7
  • 70
  • 68
  • Something is still not right...the default case is still getting triggered. – jdstein1 May 29 '13 at 16:10
  • I've seen that just now, but it's really strange that you have a "true" case, a "false" case *and* a default case. They're not mutually exclusive, what is, I think, not really appreciated by AngularJS. Remove the "false" case and replace it by the default one. – Blackhole May 29 '13 at 19:23
  • 1
    From the docs on the NgSwitchOn page >> Be aware that the attribute values to match against cannot be expressions. They are interpreted as literal string values to match against. For example, ng-switch-when="someVal" will match against the string "someVal" not against the value of the expression $scope.someVal. – keithl8041 May 14 '15 at 10:45