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 AngularJS Basics (1.x) Controllers and Scope Creating a Controller

Jonathan Martínez
Jonathan Martínez
13,375 Points

FIX: Save button not working when clicked!

In case you are having a hard time trying to understand why the ng-controller is not working when the save button is clicked, is because seems that Huston made changes to the original template without telling you.

In order for the save button to work, it must be inside the scope of the controller.

This is the original template as we were following the video:

<div ng-controller="mainCtrl" class="list">
  <input type="checkbox">
  <label class="editing-label" for="">A sample Todo!</label>
  <input class="editing-label" type="text">
</div>

<div class="actions">
  <a href="">edit</a>
  <a ng-click="helloWorld()" href="">save</a>
  <a class="delete" href="">delete</a>
</div>

See that the controller is only scoped to the div with the list class, and it's not including the div with actions class (where the save button is); so it won't work because is out of the reach of the controller.

The fix is pretty easy, just move the div with actions class inside the list div like this:

<div ng-controller="mainCtrl" class="list">
  <input type="checkbox">
  <label class="editing-label" for="">A sample Todo!</label>
  <input class="editing-label" type="text">

  <div class="actions">
    <a href="">edit</a>
    <a ng-click="helloWorld()" href="">save</a>
    <a class="delete" href="">delete</a>
  </div>
</div>

This way, the button will work because it is withing the reach/scope of the controller!

Awesome thank you for the info because I would have struggled with this.

Anthony Domina
Anthony Domina
Courses Plus Student 19,571 Points

Was banging my head against the wall. This was the exact thing I needed!

Dwayne Pate
Dwayne Pate
12,249 Points

Thanks. Completely missing that the div was closed!