What is the main purpose of directives in Angular?
Directives let you add behavior to HTML elements, making your app interactive and dynamic without changing the element's core structure.
Click to reveal answer
beginner
How do directives help in reusing code in Angular?
Directives allow you to package common behaviors and apply them to many elements, so you write code once and reuse it everywhere.
Click to reveal answer
intermediate
What types of directives exist in Angular and why are they important?
Angular has three types: components (with templates), attribute directives (change appearance/behavior), and structural directives (change layout). Each type helps manage UI in a clear way.
Click to reveal answer
intermediate
Why can't we just use plain HTML and JavaScript instead of Angular directives?
Directives provide a clean, organized way to add behavior that fits Angular's reactive model, making apps easier to build, maintain, and test compared to mixing plain HTML and JS.
Click to reveal answer
intermediate
How do directives improve the separation of concerns in Angular apps?
Directives separate UI behavior from layout and data logic, so each part of the app has a clear role, making the code easier to understand and update.
Click to reveal answer
What is a key reason Angular uses directives?
ATo create database connections
BTo replace CSS styling
CTo write server-side code
DTo add custom behavior to HTML elements
✗ Incorrect
Directives let you add custom behavior to HTML elements, making the UI interactive.
Which directive type changes the layout by adding or removing elements?
AAttribute directive
BStructural directive
CComponent directive
DService directive
✗ Incorrect
Structural directives change the layout by adding or removing elements from the DOM.
Why are directives better than plain JavaScript for UI behavior in Angular?
AThey integrate with Angular's reactive system and improve maintainability
BThey are slower but easier to write
CThey replace HTML completely
DThey only work on server side
✗ Incorrect
Directives work with Angular's reactive system, making apps easier to maintain and test.
Which of these is NOT a type of Angular directive?
AComponent directive
BAttribute directive
CDatabase directive
DStructural directive
✗ Incorrect
Database directive is not a type of Angular directive.
How do directives help with code reuse?
ABy packaging behavior to apply on many elements
BBy duplicating code in many places
CBy removing all JavaScript
DBy forcing inline styles
✗ Incorrect
Directives package behavior so you can reuse it on many elements without rewriting code.
Explain why Angular directives are needed and how they improve building web apps.
Think about how directives make HTML elements smarter and easier to manage.
You got /4 concepts.
Describe the different types of Angular directives and their roles.
Focus on what each directive type controls in the UI.
You got /3 concepts.
Practice
(1/5)
1. Why are directives needed in Angular?
easy
A. To add special behavior to HTML elements
B. To write plain CSS styles
C. To create backend APIs
D. To manage database connections
Solution
Step 1: Understand the role of directives
Directives allow you to add custom behavior to HTML elements beyond standard HTML capabilities.
Step 2: Compare with other options
Options A, C, and D relate to styling, backend, and database, which are not the purpose of directives.
Final Answer:
To add special behavior to HTML elements -> Option A
Quick Check:
Directives add behavior = B [OK]
Hint: Directives change HTML behavior, not backend or styles [OK]
Common Mistakes:
Confusing directives with CSS styling
Thinking directives manage backend logic
Assuming directives handle database tasks
2. Which of the following is the correct syntax to apply a directive in Angular?
easy
A.
B.
C.
D.
Solution
Step 1: Identify Angular directive syntax
Structural directives use the asterisk (*) before the directive name to modify the DOM structure.
Step 2: Check other symbols
# is for template references, ? and @ are invalid for directives.
Final Answer:
<div *myDirective></div> -> Option C
Quick Check:
Structural directives use * prefix = A [OK]
Hint: Look for * before directive name for structural directives [OK]
Common Mistakes:
Using # which is for template references
Using ? or @ which are invalid syntax
Omitting the * for structural directives
3. Given this Angular directive usage:
<button appHighlight>Click Me</button>
What will happen when this directive is applied?
medium
A. Nothing happens
B. The button text changes to uppercase
C. The button becomes disabled
D. The button background color changes
Solution
Step 1: Understand typical directive behavior
Directives like 'appHighlight' usually change the appearance, such as background color, to highlight elements.
Step 2: Evaluate other options
Changing text case or disabling button is less common for a highlight directive; 'Nothing happens' is unlikely if directive is implemented.
Final Answer:
The button background color changes -> Option D
Quick Check:
Highlight directive changes background = A [OK]
Hint: Highlight means color change, not disabling or text case [OK]
Common Mistakes:
Assuming text changes instead of style
Thinking directive disables element
Believing directive has no effect
4. What is wrong with this directive usage?
<div *myDirective>Content</div>
Assuming myDirective is a component, not a structural directive.
medium
A. Components cannot be used with * syntax
B. The directive name is invalid
C. The div tag cannot have directives
D. There is no closing tag
Solution
Step 1: Understand directive vs component usage
Structural directives use * syntax; components are used as elements, not with *.
Step 2: Check other options
Directive name can be valid, div can have directives, and closing tag is present.
Final Answer:
Components cannot be used with * syntax -> Option A
Quick Check:
* syntax only for structural directives = D [OK]
Hint: Use * only with structural directives, not components [OK]
Common Mistakes:
Using * with components
Thinking div can't have directives
Ignoring correct closing tags
5. You want to create a reusable directive that changes text color based on a condition. Which approach best explains why directives are needed here?
hard
A. Directives replace components for UI layout
B. Directives let you add reusable behavior to any HTML element without changing its structure
C. Directives are only for creating new HTML tags, not for behavior
D. Directives are used to fetch data from servers
Solution
Step 1: Identify directive purpose in reuse
Directives add behavior like changing text color reusable on any element without altering HTML structure.
Step 2: Eliminate incorrect uses
Directives do more than create tags, do not replace components, and do not fetch data.
Final Answer:
Directives let you add reusable behavior to any HTML element without changing its structure -> Option B
Quick Check:
Directives add reusable behavior = C [OK]
Hint: Directives add behavior reuse without new tags or data fetching [OK]
Common Mistakes:
Thinking directives only create new tags
Confusing directives with components
Assuming directives handle data fetching
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.