Custom Directives
How a custom AngularJS directive is defined, and reading one in existing code.
What you'll learn
- Explain what a directive definition object's restrict and link properties control
- Read an existing custom directive and identify its element scope and DOM-manipulation logic
- Recognize why custom directives are one of the harder parts of legacy AngularJS code to read
Explanation
Beyond the built-in directives (ng-if, ng-repeat, ng-model), AngularJS lets you define custom directives for reusable DOM behavior: app.directive("myHighlight", function() { return { restrict: "A", link: function(scope, element, attrs) { element.css("background", "yellow"); } }; });, used as <p my-highlight>Text</p>.
The returned directive definition object's restrict property controls how the directive can be used in markup: "A" (attribute, e.g. my-highlight), "E" (element, e.g. <my-highlight>), "C" (class), or a combination like "AE". The link function is where the actual DOM manipulation/event-wiring happens, given the directive's own isolated (or inherited) scope, the jqLite/jQuery-wrapped element, and the element's attrs.
Custom directives are genuinely one of the harder parts of legacy AngularJS code to read, because they mix template-declarative usage (<p my-highlight>) with imperative DOM manipulation inside link -- when investigating unfamiliar behavior on a page, checking whether an unusual HTML attribute maps to a custom directive (not a built-in one) is a common, useful step before assuming a bug is somewhere else entirely.
Guided lab
Predict: A custom directive's link function effect
This models a custom directive's link function as a plain function applied to an element's style object (this platform doesn't render real AngularJS directives). Predict the element's background after linking.
// Modeling: app.directive("myHighlight", () => ({ restrict: "A", link: (scope, element) => {...} }))
const element = { style: { background: "white" } };
function myHighlightLink(scope, element) {
element.style.background = "yellow";
}
myHighlightLink(null, element);
console.log(element.style.background);Stuck? Get a hint.
Common mistakes
- Not recognizing an unfamiliar HTML attribute as a reference to a custom directive, and searching for the bug in the wrong place entirely.
- Confusing restrict: 'A' (attribute usage) with restrict: 'E' (element usage) when reading or writing a directive's markup usage.
- Assuming all DOM manipulation in an AngularJS app happens through ng-* built-ins -- custom directives' link functions often do direct DOM manipulation too.
Knowledge check
Takeaway
When investigating unfamiliar page behavior, check whether an unusual HTML attribute maps to a custom directive before assuming the bug is elsewhere.
Summary
Custom directives extend AngularJS's template vocabulary; restrict controls usage form (attribute/element/class), and link performs the actual DOM manipulation.
References
Your notes
Notes save automatically.
Finished this lesson?
Mark it complete to track your progress and schedule a future review.
AI tutor
The optional AI tutor isn't enabled in this deployment. All lessons, exercises, quizzes, and search work fully without it.