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 An Introduction to Two-Way Data Binding Two-Way Binding: watchers

Getting a "parse error" for my code. What parameters needs to be included within the $watch function?

I've matched pretty closely to the $watch example in the video, and it does not execute.

app.js
angular.module('myApp', [])
.controller('myController', function ($scope, $http) {
  $scope.user = {name: 'Alex', id: 123};

  getUserData = function (id) {
    console.log('Getting user with ID: ' + id);
  }

  $scope.$watch(user.id, function(newID){
    if (newID) {getUserData(newID)}
  }

  // YOUR CODE HERE

});
index.html
<!DOCTYPE html>
<html ng-app="myApp">
<head>
  <title>Angular.js</title>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
  <script src="app.js"></script>
</head>
<body ng-controller="myController">

  <input type="text" ng-model="user.id" />

</body>
</html>

2 Answers

Colin Bell
Colin Bell
29,679 Points
  1. You want to pass in user.id as a string.
  2. You're forgetting a closing parentheses.
//                 v-- this needs to be a string
  $scope.$watch('user.id', function(newID){
    if (newID) {getUserData(newID)}
  }) // <-- Closing parentheses here

Thanks Colin, caught that and should have updated the question. Silly error but I appreciate the help.