0

Assume we have this JS code:

.directive('myCustomer', function() {
 return {
    template: 'Name: {{customer.name}} Address: {{customer.address}}'
  };
});

and html code:

<div my-customer></div>

Can anyone tell me why we are matching directive with div using my-customer while we don't have this name anywhere in the code?

above code is from hereDeveloper Guide / Directives

1
  • This is a common gotcha with everyone who is new to angular. HTML is not case-sensitive and camelCase is the norm in JS since using - in a variable name means you can't reference it like someObj.my-customer so there is this normalization process between the two. Commented Jan 26, 2015 at 7:40

1 Answer 1

3

Angular normalizes an element's tag and attribute name to determine which elements match which directives. We typically refer to directives by their case-sensitive camelCase normalized name (e.g. ngModel). However, since HTML is case-insensitive, we refer to directives in the DOM by lower-case forms, typically using dash-delimited attributes on DOM elements (e.g. ng-model).

https://docs.angularjs.org/guide/directive#normalized

Sign up to request clarification or add additional context in comments.

2 Comments

docs.angularjs.org/guide/directive#matching-directives <-- this section in that page also explains about how directives are matched regarding stripping the x- or data- prefixes.
@shaunhusain Yes, you are correct! Thanks for improving the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.