2

I am new to Angular. I am trying to do switch the div based on some condition :

  <td>
    <div layout="row" layout-align="start center">
      <img ng-if="row.pin" ng-src="{{pinImageUrl}}" alt="Pinned @ {{row.pin}}" class="img-pin" />
      <div ng-switch="checkUrl">
        <a ng-switch-when="{{isNumber(row.id)}}" href="numberUrl ">
          {{getName(row)}}
        </a>
        <a ng-switch-when="url" href="{{url + row.id}}">
          {{getName(row)}}
        </a>
        <span ng-switch-default href="aaaaaa">
          {{getName(row)}}
        </span>
      </div>
    </div>
  </td>

But {{isNumber(row.id)}} not working. Can someone tell me what is wrong here ?

CozyAzure
  • 8,280
  • 7
  • 34
  • 52
user3407267
  • 1,524
  • 9
  • 30
  • 57

1 Answers1

0
  <div ng-switch="checkUrl">
    <a ng-switch-when="{{isNumber(row.id)}}" href="numberUrl ">
      {{getName(row)}}
    </a>
    <a ng-switch-when="url" href="{{url + row.id}}">
      {{getName(row)}}
    </a>
    <span ng-switch-default href="aaaaaa">
      {{getName(row)}}
    </span>
  </div>

The expression in the ng-switch can be a scope function call, scope variable or can be a value. But in ng-switch-when="something", here something should be a value. It didn't work even if we mention scope variable or function call. What you mention, it takes it as a value and then compares the value in the ng-switch then renders if matches. But in your code, you are calling a function isNumber() in ng-switch-when. So, it is not matching. Even in the second case, it should be a value of url, not variable. But if you want to use functions to render the DOM, go for ng-if instead of ng-switch. ng-if="funcA()" will work, but executes(watches) on every digest cycle to know the value. But calling a function inside ng-if is also not a good solution. For more info ng-if being called more times than it should

Community
  • 1
  • 1
Mr_Perfect
  • 8,254
  • 11
  • 35
  • 62