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"' ?

angular.module('treehouseCourse', [])
  .directive('sometimesTwoWay', function() {
    return {
      require: '?ngModel'
<!DOCTYPE html>
<html ng-app="treehouseCourse">
  <script src="js/angular.js"></script>
  <script src="app.js"></script>

  <sometimes-two-way ng-model="course.description"></sometimes-two-way>


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
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.



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'