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 trialBranko Dragovic
26,314 Pointscheck is controller defined
can somebody help me with this task please? I dont get it :/
angular.module('treehouseCourse', [])
.directive('sometimesTwoWay', function() {
return {
// YOUR CODE HERE
require: '?ngModel',
link: function($scope,$element,$attrs,ngModelCtrl){}
}
});
<!DOCTYPE html>
<html ng-app="treehouseCourse">
<head>
<title>Angular.js</title>
<script src="js/angular.js"></script>
<script src="app.js"></script>
</head>
<body>
<sometimes-two-way ng-model="course.description"></sometimes-two-way>
</body>
</html>
2 Answers
Iain Simmons
Treehouse Moderator 32,305 PointsThe controller should be the value passed to the parameter ngModelCtrl
in the link
function. It will either be defined (passed a value) or by default, will be undefined
, as any parameter not passed into a function would be in JavaScript.
So you're essentially testing if ngModelCtrl
equals undefined
.
The simplest way to do that is a conditional with a logical NOT operator:
if (!ngModelCtrl) {
// ngModelCtrl is undefined
}
OR of course, you can leave out the !
and check first to see if it is defined:
if (ngModelCtrl) {
// ngModelCtrl is defined
} else {
// ngModelCtrl is undefined
}
Hope that helps!
Branko Dragovic
26,314 PointsThank you Iain :)
Iain Simmons
Treehouse Moderator 32,305 PointsNo problem. You should probably mark the answer as 'Best Answer' if it answered your question, that way other students know your question has been answered/closed.