The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

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"