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 Extending Inputs Extending Inputs: Optional require

Why would a directive want to optionally require the NgModelController -- when it really is requiring the ngModel?

Q: Set the 'sometimesTwoWay' directive up to optionally require the NgModelController.

A: require: '?ngModel'

Why is the answer not 'require: "?ngModelController"' ?

app.js
angular.module('treehouseCourse', [])
  .directive('sometimesTwoWay', function() {
    return {
      // YOUR CODE HERE
      require: '?ngModel'
    }
  });
index.html
<!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>

5 Answers

The require property tells Angular that our directive requires the controller of another directive. In this case, we want to use the ngModel controller

I'm not quite sure how to answer this so I hope someone can answer your question more clearly for you soon! But have you looked at angular's documentation for this yet? Hopefully it can help clear up the differences between ngModel and NgModelController.

https://docs.angularjs.org/api/ng/directive/ngModel

https://docs.angularjs.org/api/ng/type/ngModel.NgModelController

According to the angular documentation:

The ngModel directive binds an input,select, textarea (or custom form control) to a property on the scope using NgModelController, which is created and exposed by this directive.

Could someone please tell me the exact answer for this? I can't get my code to pass. Thanks.

require: '?ngModel'

Just this :

require : '?ngModel'