Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am new to angular.

My question is, Is it possible to replace 'ng-hide' with 'ng-show' if 'ng-if' is satisfied?

I want to hide and show certain div based on the data I receive. Its either 'true' or 'false', if 'false' the div should be hidden, else it should show.

share|improve this question
    
what's the problem with ng-if/ng-show itself..?? your question seems unclear – Pankaj Parkar yesterday
    
... you shouldn't need to. use ng-show or ng-hide if you want the element to always exist but show or hide based on a value, else use ng-if if you want the element to exist or not exist based on a value. – Kevin B yesterday
    
That is where my problem is. I have 2 sets of data. Initially i want to hide both of them, and later after a validation, i get to show either one of the 2 data in a div. – Atul Kumar yesterday
    
Could you provide JSFiddle or Plunker? It's not easy to figure out what you need to achieve. – Pneumokok yesterday
    
you only need one of the three directives, and a conditional that has the logic you seek. It could be as simple as ng-if="foobar" where foobar is a property on your scope that you change based on the logic you need. – Kevin B yesterday

It can be done with boolean logic:

<div ng-show="cond1 && cond2">
    One data set
</div>
<div ng-show="cond1 && !cond2">
    Other data set
</div>

Both data sets will be hidden if cond1 == false. If true, one or the other will show.


Or use De Morgan's Theorem:

<div ng-show="cond1 && cond2">
    One data set
</div>
<div ng-hide="!cond1 || cond2">
    Other data set
</div>
share|improve this answer
    
This solution was very helpful. Thank you! – Atul Kumar 11 hours ago

It is unclear for me what you need to achieve, but maybe it will be useful:

function Controller (SomeService) {
    var vm = this;
    vm.firstVisible = false;
    vm.secondVisible = false;
    SomeService.getData().then(data) {
        // firstValidation() function returns true if first way of display is matching
        if (firstValidation(data)) {
            vm.firstVisible = true;
        }

        // secondValidation() function returns true if second way of display is matching
        if (secondValidation(data)) {
            vm.secondVisible = true;
        }
    }
}

And then in HTML:

<div>
    <div ng-if="vm.firstVisible">
        First way of displaying data
    </div>
    <div ng-if="vm.secondVisible">
        Second way of displaying data
    </div>
</div>

(Intentionally I don't use $onInit or other stuff to do not make things complicated)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.