Frage

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>
War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top