Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript AngularJS Extending Inputs Extending Inputs: Optional require

eric francis
eric francis
5,705 Points

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

Bryan Knight
Bryan Knight
34,214 Points

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

Kristopher Van Sant
PLUS
Kristopher Van Sant
Courses Plus Student 18,830 Points

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'