1

I have this codes

.state('fooState', {
    url: '/foo',
    templateUrl: 'path/to/bar.html',
    controller:'parentController'
})

// THIS IS THE bar.html
<div class="sample">
    <div ng-show="bazinga" ng-controller="child1Controller">
        <!-- cool stuff -->
    </div>
    <div ng-show="!bazinga" ng-controller="child2Controller">
        <!-- cool stuff -->
    </div>
</div>

Is it normal that when the first div with the controller child1Controller is shown, the controller child2Controller will also run? How do I prevent a controller from running if it is hidden via ng-show="false"? I only need the parentController and the child1Controller controllers running when the first div is visible. Same with when 2nd div is visible, the parentController and the child1Controller controllers should only the one's running.

2
  • 1
    You could also do this with nested states/views... Commented Jan 15, 2016 at 20:22
  • So there would be another <div ui-view> ? Commented Jan 16, 2016 at 2:25

1 Answer 1

2

If I'm not mistaken, using ng-if rather than ng-show should solve your issue. The difference being that ng-if does not render the element to the DOM when it evaluates to false.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi thanks, so how do I exactly do the logic? What will be the value for ng-if?
It's used the same as how you're using ng-show, so literally just swap those out and the values stay the same

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.