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 trial1,329 Points
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.
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
});
<!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
29,679 Points- You want to pass in
user.id
as a string. - 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
1,329 Points
Thanks Colin, caught that and should have updated the question. Silly error but I appreciate the help.