352229 (9) [Avatar] Offline
#1
Hi

I am using following sample code for checking ng-if and ng-else.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
    <script type="text/javascript">
        var app = angular.module('app', []);
        app.controller('myController', function ($scope) {
            $scope.Voters = [
            { Name: "Karan", Age: 33 },
            { Name: "Anurag", Age: 16 },
            { Name: "Ritesh", Age: 34 }
            ];
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div ng-app="app" ng-controller="myController">
            <table>
                <tr ng-repeat="voter in Voters">                  
                    <td ng-if="voter.Age >= 18">
                            {{voter.Name}} + {{voter.Age}}
                    </td>                   
                    <td  ng-else="voter.Age < 18">
                            {{voter.Name + " is not eligible to vote"}} + {{voter.Age}}
                    </td>                     
                </tr>
            </table>
    </div>
    </form>
</body>
</html>



But I am getting strange output. Please check the file attached.

My question is why ng-else block is displaying
Lukas Ruebbelke (107) [Avatar] Offline
#2
There is no ng-else. You need to use ng-if and invert your expression i.e. ng-if="!someValue"