Home > front end >  Angular ng-switch-when not showing
Angular ng-switch-when not showing

Time:08-18

Can anyone tell me what I'm doing wrong with my ng-switch statement. I can never get the second condition to fire. I've verified that SearchDS.maxPages() contains the Max Number of Pages so at some point that section should be printed.

<div>
  <div ng-switch="SearchDS.pagingOptions.currentPage">
    <p ng-switch-when="1">1</p>
    
    <p ng-switch-when="{{SearchDS.maxPages()}}">2</p>
    
    <p ng-switch-default>3</p>
  </div
</div>

CodePudding user response:

You dont need the {{}} for that expression.

CodePudding user response:

You cannot use expressions inside ng-switch-when instead use a function that will perform the switch case internally and exhibit the same behaviour!

angular.module('app', [])
  .controller('ctrl', function() {
  this.currentPage = 12;
 this.getValue = () => {
    const secondCase = 12;
    const firstCase = 1;
    switch (this.currentPage) {
      case firstCase:
        return '1';
      case secondCase:
        return '2';
      default:
        return null;
    }
  }
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl as $ctrl">
  <div>
    <div ng-switch="$ctrl.getValue()">
      <p ng-switch-when="1">1</p>

      <p ng-switch-when="2">2</p>

      <p ng-switch-default>3</p>
    </div>
  </div>
</div>

  • Related