Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Cameron Bourke
Cameron Bourke
10,198 Points

AngularJS Controllers, of lack of Controllers.

I know treehouse hasn't got any courses on AngularJS, besides that one workshop, but I'm sure a fair few folk on here have some experience with the magical framework that angular is.

I'm trying to build an interface where you can tab on different links and it shows you the corresponding info. Here's a pen of it working:

http://codepen.io/cambourke/pen/rzeGh

However, there is too much logic in my markup, so I need to wrap up that logic and store it in a controller. And thats exactly what I'm trying to achieve with the pen below, but with no luck!

http://codepen.io/cambourke/pen/HxbLe

It's frustrating when the method that's not best practice works haha. Any help would be greatly appreciated!

Cheers.

1 Answer

Hi. Was just looking at your code. I see the issue. It has to do with scope. You are using the new controller in the <nav> tag so it is only available to that element. A quick fix is to move the ng-controller="TabController as tab" to outer div <div class="row" ng-controller="TabController as tab"> and that should fix your problem. This allows the other elements inside that div to have access to that new controller. Really like your design in the code-pen pages clean and simple.

cheers.

Cameron Bourke
Cameron Bourke
10,198 Points

Awesome. Such a rookie mistake, thanks for picking that up for me man! Makes sense that if I'm calling directives like <ul class="list-break" ng-show="tab.isSet(2)"> for the TabController that the scope needs to include all the unordered lists on the page aswell. Thanks again!

Cheers.