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 trialTravis Thompson
12,976 PointsWithin your link function's body, set up a conditional to test whether ngModelCtrl - STUMPED
Not sure why this is not passing. The conditional seems to be setup correctly.
Question is: Within your link function's body, set up a conditional to test whether ngModelCtrl (which you should have set up as the 4th parameter) is defined. If it isn't, the function should return immediately. If it is defined, call console.log('my model', ngModelCtrl).
My Code:
angular.module('treehouseCourse', [])
.directive('sometimesTwoWay', function() {
return {
// YOUR CODE HERE
require: '?ngModel',
link: function($scope, $element, $attrs, ngModelCtrl) {
if (ngModelCtrl === undefined) {
return;
} else {
console.log('my model', ngModelCtrl);
}
}
}
});
1 Answer
Chris Shaw
26,676 PointsHi Travis,
Your code won't work because undefined refers to variables that that psychically doesn't exist which isn't the case here since ngModelCtrl
is defined as a parameter, instead all you need to do is use the bang !
operator to check if the value is anything other than true.
Also I prefer not to have else
statements where a return
statement exists as it's redundant since a return in-between execution is already explicit.
angular.module('treehouseCourse', [])
.directive('sometimesTwoWay', function() {
return {
// YOUR CODE HERE
require: '?ngModel',
link: function($scope, $element, $attrs, ngModelCtrl) {
if (!ngModelCtrl) {
return;
}
console.log('my model', ngModelCtrl);
}
}
});
Happy coding!