0

I'm new to Angular and I want to hide some specific HTMl in case of an Object is empty, but how can I achieve that?

so far I have:

$scope.links = data.links;

and then:

<div ng-if="links">
    /// some more html
</div>

but that doesnt work. Any help is appreciated...

6
  • What do you mean by empty? What type of object is data.links? Commented Jul 2, 2015 at 12:19
  • ng-if= 'links != []' Commented Jul 2, 2015 at 12:20
  • 1
    @upendrasinghsengar That's not a valid test Commented Jul 2, 2015 at 12:22
  • @RGraham sometimes, console.log($scope.links) returns Object {} Commented Jul 2, 2015 at 12:22
  • try this one ng-if= '{{links != []}}' Commented Jul 2, 2015 at 12:23

5 Answers 5

1

Controller:

$scope.linksLength = Object.keys(data.links).length > 0;

HTML:

<div ng-show="linksLength">
    /// some more html
</div>
Sign up to request clarification or add additional context in comments.

Comments

1

In cases like this I would write a simple function to use:

$scope.showLinks = function () {

  return $scope.links !== undefined && $scope.links !== null && $scope.links.length > 0;
}

then

<div ng-if="showLinks()">
    /// some more html
</div>

1 Comment

Assuming links is indeed an array, ng-if="links.length" would be fine as the view code chomps exceptions if the expression is undefined.
0

Just add .length to ng-if="links" then change ng-if to ng-show.

E.g.

<div ng-show="links.length">
    /// some more html
</div>

If ng-show detects that the value of links.length is 0 then it's false if not equal to 0 then it's true.

0 = false

> 0 = true

Hope it helps.

Comments

0

use ngHide or ngShow.

<div ng-show="links.length">

https://docs.angularjs.org/api/ng/directive/ngHide

Comments

0
<body>
<div ng-app="myApp" ng-controller="myCtrl" >
<p ng-hide="isempty(products)">element</p>
</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {

$scope.products=[];

$scope.isempty=function(obj)
{
return (angular.equals([], obj));
}
});
</script>
</body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.