ng-switch + function

0

Good afternoon, I am trying to use ng-switch to show a value that depends on a condition that is in a function, but I can only pass fixed values to the field, the function with if does not seem to pass the value , if not clear, follow the example in codepen.

$scope.value1='0';
  $scope.condition = function(){
    if($scope.value1>=10){
      return $scope.value=200;
    }
  }

link

    
asked by anonymous 15.09.2014 / 22:33

1 answer

0

Translating a part of the ng-switch documentation :

  

Please be aware that the values for the [ ng-switch-when ] attribute can not be expressions . Values are interpreted as literals to be compared.

That is, when you use ng-switch-when="condition()" , the angle is trying to compare the value of the ng-switch attribute with the condition() string instead of calling the function.

In your case, I would use ng-show and ng-hide to display an alternative condition to ng-switch :

<body ng-app ng-controller='controller'>
    <label>Type a value: <input type="text" ng-model="value" /></label><br />
    <div ng-switch="value" ng-hide="condition()">
        <div ng-switch-when="1">1</div>
        <div ng-switch-when="3">3</div>
        <div ng-switch-when="4">4</div>
        <div ng-switch-when="5">5</div>
        <div ng-switch-default>None</div>
    </div>
    <div ng-show="condition()">2</div>
</body>

You can also do this within ng-switch-default :

<body ng-app ng-controller='controller'>
    <label>Type a value: <input type="text" ng-model="value" /></label><br />
    <div ng-switch="value">
        <div ng-switch-when="1">1</div>
        <div ng-switch-when="3">3</div>
        <div ng-switch-when="4">4</div>
        <div ng-switch-when="5">5</div>
        <div ng-switch-default>
            <div ng-show="condition()">2</div>
            <div ng-hide="condition()">None</div>
        </div>
    </div>
</body>
    
15.09.2014 / 23:27