4

This is so strange and I have no idea why this simple scenario isn't working. I'm still fairly new to Angular after a life with jQuery and Backbone.

Here's the basic structure of the code...

<div class="span10" ng-controller="ClientCtrl" ng-hide="addClient">
  <button ng-click="addClient = true" class="btn btn-primary">
    Add new Client
  </button>
</div>
<div class="span10" ng-controller="ClientCtrl" ng-show="addClient">
  <div ng-include src="'views/partials/client.html'"></div>
</div>

The expected behavior is that clicking the 'Add New Client' button will show the second div. The actual behavior is that the first div gets hidden (as it should) but the second div stays hidden. There is no controlling behavior in the controller.

I've tried it with a scope function, setting the initial in the property in the controller and numerous other things without any luck. What am I doing wrong? I've checked to be sure they are on the same scope and that doesn't "seem" to be the problem. Thanks for the help because this is driving me crazy.

2
  • When you say "stays hidden" did you check if the ng-include is not working or does the corresponding parent div actually remain hidden? Try placing some static code inside the second div to see if the ng-include is the one failing or is it indeed the ng-show... Commented Jun 28, 2013 at 14:52
  • Yes, the nginclude is fine. Before click: d.pr/i/VIye and after click: d.pr/i/rSEP Commented Jun 28, 2013 at 14:55

1 Answer 1

12

AngularJS creates two separate Controllers in this case as you have mapped two controllers using ng-controller attribute. The scope within one controller does not affects the scope of second.

You must do something like this:

<div ng-controller="ClientCtrl">
  <div class="span10" ng-hide="addClient">
    <button ng-click="addClient = true" class="btn btn-primary">
      Add new Client
    </button>
  </div>
  <div class="span10" ng-show="addClient">
    <div ng-include src="'views/partials/client.html'"></div>
  </div>
</div>
Sign up to request clarification or add additional context in comments.

3 Comments

That's the answer! I should have realized that but never tried it. Will accept the answer when SO allows. Oh, and thank you.
Yep. Controllers are not injected as shared instances (singletons) by Angular (although services are!)
Thanks a lot for this answer. It's almost two years later, but the answer is still relevant, spent A LOT of time on this before finally coming across this. AngularJS can be really frustrating sometimes!

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.