3

I have following problem with ngShow. I receive response as a jSON from $http.get and construct several DOM elements using ngRepeat. All this working properly. From my controller a just apply:

    $http.get(requestUrl).success(function (data) {
        $scope.results = data.results;
    });

data.results is a object like this:

{ 
   "someProp": ["item1", "item2", "item3"],
   "someProp1": ["item1", "item2", "item3"]
}

From my template I try to use ngShow like this:

<table ng-show="Object.keys(results).length > 0">

and like this:

<table ng-show="Object.keys($scope.results).length > 0">

With no luck.

<table ng-show="true">

and

<table ng-show="false">

working properly.

So it seems that the problem is in the expression. I would be very grateful for any help.

Georgi Naumov
  • 4,160
  • 4
  • 40
  • 62
  • 3
    `ng-show` expects an [angular expression](http://docs.angularjs.org/guide/expression), so without doublechecking you can't assume that every *normal* javascript function will just work. – Yoshi Aug 28 '13 at 14:10
  • wrap `Object.keys(results).length` in a function instead. – zs2020 Aug 28 '13 at 14:17

4 Answers4

8

It does not evaluate the Object.keys function inside of the expression as it is not actually located on the scope. One way you can get around this is by assigning the Object to the scope.

$scope.Object = Object;

and inside your view

<div ng-show="Object.keys(results).length > 0">
    {{Object.keys(results).length}}
</div>
Mark Coleman
  • 40,542
  • 9
  • 81
  • 101
  • 1
    Thank you! I've been scratching my head for a while why this didn't work. – Arthur Frankel Feb 09 '14 at 01:53
  • where do you set $scope.Object = Object; I got error?? http://stackoverflow.com/questions/27311331/how-to-get-the-length-of-a-map-in-angularjs-ng-show – Jaxox Dec 05 '14 at 08:32
2

Ng Show needs an instruction, you have to surround it with simple quotes

<table ng-show="'Object.keys(results).length > 0'">
Nox
  • 236
  • 1
  • 9
0

You are storing data.results to $scope.results. Hence your scope variable will not have results. If you want to check for results, then use the following.

$http.get(requestUrl).success(function (data) {
        $scope.backEndJson = data;
});

Hence your HTML should have,

<table ng-show="backEndJson(results).length > 0">

BTW, we are not supposed to use $scope on the HTML side as $scope as all operations on the HTML side is assumed to be on the scope variable.

Abilash
  • 6,089
  • 6
  • 25
  • 30
0

Try this:

<table ng-hide="angular.equals({}, results)">