0

I want to show dynamically the REST respont data after an AngularJS $http.get() call. The answer arrives fine The data bindings {{showOrderStateTrigger}} and {{orderDetails}} inserted just for test purposes after the call display true and the expected contents. But the ng-show and ng-hide does not recognize that their condition changed. I created ng-show and ng-hide cases just to test two approaches. Checking that the variable undefined or a boolean variable state. I tried the $scope.$apply() call, but it just made the things worse. The nested calls caused exceptions. Can anybody help me to solve this problem?

        <script type="text/javascript">
            var myApp = angular.module( "myApp", [] );
            myApp.controller( "myController", function( $scope, $http ) 
            { 
              $scope.showOrderDetailsTrigger = false;
              $scope.showOrderDetails = function ( id )
              {
                  $http.get( "/WebApplication1/orders/" + id + ".json" )
                       .then( function ( response ) {
                           $scope.orderDetails = response.data;
                           $scope.showOrderDetailsTrigger = true; } );
              }
            } )
        </script>
    </head>

    <body ng-app="myApp" ng-controller="myController">
        <ul>
            <c:forEach var="order" items="${orders}">
                <li ng-click="showOrderDetails( ${order.id} )">${order.name}</li>
            </c:forEach>
        </ul>

        <div>{{showOrderDetailsTrigger}}</div>
        <div>{{orderDetails}}</div>

        <div ng-show="{{showOrderDetailsTrigger}}">
            <div>ID: {{orderDetails.id}}</div>
            <div>Name: {{orderDetails.name}}</div>
        </div>

        <div ng-hide="{{orderDetails === undefined}}">
            <div>ID: {{orderDetails.id}}</div>
            <div>Name: {{orderDetails.name}}</div>
        </div>

    </body>
</html>
SOLID Developper
  • 672
  • 4
  • 12

1 Answers1

1

Many directives like ng-show , ng-hide and ng-if expect expressions that resolve truthy or falsy not interpolation expressions

try changing

ng-show="{{showOrderDetailsTrigger}}"

to

ng-show="showOrderDetailsTrigger"

For more advanced conditions would be something like:

ng-if="somVar != 3 && foo=='bar'"
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • Uhhh. That's right. Trivial "mistyping" fault caused by careless accuracy. Thanks a lot. – SOLID Developper May 26 '18 at 22:30
  • No worrries. is not always intuitive. Newer angular 2+ is more consistent – charlietfl May 26 '18 at 22:32
  • @Bitman In your case, you really should consider using `ng-if` instead of `ng-show`. `ng-if` removes the template from the view, `ng-show` only hides it. You will have orderDetails evaluated to undefined that will lead to an error – Deblaton Jean-Philippe May 26 '18 at 22:37
  • @DeblatonJean-Philippe I don't thing so. After the first click it should appear and stay in that state. It should show always the contents of the very last clicked list item. – SOLID Developper May 26 '18 at 22:41
  • @Bitman I still do think so. Your code should throw an error as far as id does not exist on undefined. `ng-if` won't change you app's behaviour – Deblaton Jean-Philippe May 26 '18 at 22:43
  • @DeblatonJean-Philippe shouldn't throw any error. – charlietfl May 26 '18 at 22:45
  • @DeblatonJean-Philippe I works fine for me for multiple clicks as well! :) – SOLID Developper May 26 '18 at 22:47
  • 1
    @DeblatonJean-Philippe AngularJS Expressions are **forgiving**. In JavaScript, trying to evaluate undefined properties generates `ReferenceError` or `TypeError`. In AngularJS, expression evaluation is forgiving to `undefined` and `null`. For more information, see [AngularJS Developer Guide - AngularJS Expressions vs. JavaScript Expressions](https://docs.angularjs.org/guide/expression). – georgeawg May 26 '18 at 23:38
  • @georgeawg ok, my bad ;-) – Deblaton Jean-Philippe May 26 '18 at 23:43