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 An Introduction to Two-Way Data Binding Two-Way Binding: ngModelCtrl

Location of my-directive in <div>

Location of my-directive in <div> seems to cause a red error condition in a workspace. Should it be after ng-model?

Changing from: <div my-directive ng-model="user.name"></div> to: <div ng-model="user.name" my-directive ></div>

1 Answer

Igor Yamshchykov
Igor Yamshchykov
24,397 Points

you may add directive directive in three different ways, in this case it should be added like so

  <div my-directive ng-model="user.name"></div>

but there are also three different ways of adding directive to your html

depending on the restrict property you've added in the directive script like so:

.directive('myCustomer', function() {
  return {
    restrict: 'E',
    templateUrl: 'my-customer.html'
  };
});

restict property could be E, A, C or M

'A' - only matches attribute name 'E' - only matches element name 'C' - only matches class name 'M' - only matches comment you can also combine them like so restrict: 'AEC'

so if you set the restrict property

.directive('myCustomer', function() {
  return {
    restrict: 'E',
    templateUrl: 'my-customer.html'
  };
});

then your html should look like this

  <my-directive> </my-directive>

Hope that makes sence.