Angular SA Solution
Angular SA Solution
Select the correct form control class name which is set to true in Angular 9
whenever value is modified.
a) ng-invalid
b) ng-pending
c) ng-pristine
d) ng-dirty
Correct Answer:
D) ng-dirty
Detailed Explanation:
• In Angular Forms, every form control automatically tracks its own state.
• When the user modifies the value of a form control (like typing something in an
input box), Angular marks that control as "dirty".
• When this happens, Angular applies the CSS class ng-dirty to the input element.
• "Dirty" here means "value has been changed" from its original state.
So, whenever you edit or type into a field ng-dirty becomes true!
• A) ng-invalid → Wrong!
➔ This is applied when validation fails, not when value is modified.
• B) ng-pending → Wrong!
➔ This shows up when the control is waiting for async validation (like checking
username availability), not just after editing.
• C) ng-pristine → Wrong!
➔ Pristine means "untouched", no user modification yet.
➔ As soon as you modify the value, pristine becomes false and dirty becomes true.
Pro Tip:
Whenever user types, clicks, or edits the field ➔ Think "dirty" = ng-dirty!
(Just like dirty hands after writing — value is no longer clean/pristine!)
Question 2
Which component will be invoked for path "/search"?
Consider the below code:
Hint:
Correct Answer:
B) SearchComponent
Detailed Explanation:
Let's walk through the route configuration carefully:
• A) HomeComponent → Wrong!
➔ HomeComponent is loaded only when navigating to /home or when accessing the
root / (because of { path: '', redirectTo: 'home' }).
• C) AboutComponent → Wrong!
➔ AboutComponent would be loaded only for /about path.
• D) FindComponent → Wrong!
➔ Notice, there is no FindComponent at all!
➔ The path find is redirected to search, and we display the SearchComponent
instead.
redirectTo is only for changing the URL, not rendering a component directly.
If no pathMatch is specified, default is 'prefix', but for empty path ('') matching, we
should use pathMatch: 'full'.
• redirectTo ➔ Only redirects the URL, does NOT render a component itself .
• pathMatch: 'full' ➔ Important for root path ('') redirects!
• For exact URL like /search ➔ Direct match → corresponding component is invoked
.
• There’s no component called FindComponent here, just a redirect!
Pro Tip:
When you see redirectTo, follow the path!
Think like a detective :
• Where is it pointing?
• What does that new path load?
Final Thought:
Navigating to /search → loads SearchComponent
Question 3
Consider the below TypeScript code:
class Car {
public id: number = 10;
public name: string;
constructor();
constructor(name: string);
constructor(car: Car);
constructor(car?: any) {
if (typeof (car) == "string") {
this.name = car;
} else {
this.id = car.id;
this.name = car.name;
}
}
}
What are the valid ways to create an object for the Car class?
MCQ:
Given the TypeScript class Car, what are the valid ways to create an object?
Options:
A) var car1 = new Car();
B) var car2 = new Car(1);
C) var car3 = new Car("Audi");
D) var car4 = new Car({ name: "Audi", id: 10 });
Correct Answer:
A) car1, C) car3, D) car4
So correct choices are A, C, and D
Detailed Explanation:
constructor();
constructor(name: string);
constructor(car: Car);
constructor(car?: any) {
if (typeof (car) == "string") {
this.name = car; // If string is passed ➔ assign to name
} else {
this.id = car.id; // If object is passed ➔ copy id
this.name = car.name; // copy name
}
}
Concept Meaning
Constructor TypeScript allows multiple constructor signatures but only one
Overloading actual implementation
typeof operator Used at runtime to check types like "string", "number",
"object"
car?: any The parameter is optional (?) and can be of any type (any)
Object checking In TypeScript/JS, objects have properties (numbers don't!)
TypeScript Rule:
Pro Tip:
In exams, always check the data type passed in constructors for overloaded signatures!
If a number is passed where an object/string is expected, it usually causes error!
Final Thought:
Question 4
Which of the below statements will iterate through the employees array?
a)
b)
c)
d)
MCQ:
Which of the following statements will correctly iterate through the employees array in
Angular?
Options:
A)
B)
C)
D)
Correct Answer:
A) <tr *ngFor='let employee of employees'>
Detailed Explanation:
Remember:
• *ngFor ➔ iteration
• *ngIf ➔ show/hide based on condition
• ngModel ➔ binding form inputs
Final Thought:
Question 5
Which of the below statements will iterate through the employees array?
a)
b)
c)
d)
<tr *ngIf='let employee of employees'>
<td>{{employee.code}}</td>
<td>{{employee.name}}</td>
<td>{{employee.gender}}</td>
<td>{{employee.annualSalary}}</td>
<td>{{employee.dateOfBirth}}</td>
</tr>
MCQ (Q5):
Which of the following statements will correctly iterate through the employees array?
Options:
A)
B)
C)
D)
Correct Answer:
A) <tr *ngFor='let employee of employees'>
Detailed Explanation:
• *ngFor is the Angular structural directive used for looping over arrays.
• The only correct syntax is:
• *ngFor="let item of collection"
• Here:
o let employee creates a local variable for each iteration.
o of employees specifies the collection (array) to loop through.
• Therefore, in Option A, you properly loop over each employee in employees!
Other Options Analysis:
Pro Tip:
Whenever you see for used in an Angular template ➔ ALARM bells!
➔ Angular never uses for — only let ... of ...!
Question 5
Tina wants to invoke an API service that will return the price of a given book. The
signature of the API method is as shown below:
[HttpGet]
public float GetPrice(Int bookId);
a)
this.http.get<float>(serviceUrl).body({bookId=3526});
b)
c)
d)
this.http.get<float>(serviceUrl + '?bookId=3526');
Alright!
This one is a super important MCQ because it checks your understanding of HTTP GET
requests in Angular and API parameter passing — something that's directly used in real-
world Angular projects!
Options:
A)
this.http.get<float>(serviceUrl).body({bookId=3526});
B)
C)
D)
this.http.get<float>(serviceUrl + '?bookId=3526');
Correct Answer:
D) this.http.get<float>(serviceUrl + '?bookId=3526');
Detailed Explanation:
• In HTTP GET requests, parameters are passed through the URL query string .
• Syntax for a query string:
➔ ?key=value (first parameter)
➔ &key2=value2 (for additional parameters)
• So, if serviceUrl is like http://example.com/api/GetPrice,
then to pass bookId=3526,
final URL becomes:
http://example.com/api/GetPrice?bookId=3526
• In Angular's HttpClient, you simply append parameters to the URL for GET
requests like shown in Option D .
this.http.get<ReturnType>(url + '?param1=value1¶m2=value2');
Pro Tip:
In real-world Angular projects , a better way is to use HttpParams instead of manually
appending query params:
(But for MCQs — appending to URL is enough and matches your option D perfectly!)
Final Thought:
Question 6
If you provide a custom service in two components’ providers section of the
@Component decorator, how many instances of the service shall get created?
a) 1
b) 2
c) 3
d) 4
MCQ (Q6):
If you provide a custom service in two components' providers array inside the
@Component decorator, how many instances of the service will be created?
Options:
A) 1
B) 2
C) 3
D) 4
Correct Answer:
B) 2
Detailed Explanation:
• In Angular, services are singleton by default, ONLY IF they are provided in root
(providedIn: 'root') or at module level.
• BUT... if you provide a service in the providers array of a @Component
decorator,
➔ a new instance of the service is created for that component and its children!
• So here:
o Component 1 provides the service → gets its own instance
o Component 2 provides the service → gets its own separate instance
• Thus, TWO SEPARATE instances are created — one for each component!
Simple Picture:
• A) 1 → Wrong!
➔ Would be correct only if service was provided in root or at module level, not at
component level.
• C) 3 → Wrong!
➔ Only 2 components are involved, so 3 instances make no sense!
• D) 4 → Wrong!
➔ Definitely not! No such rule in Angular.
Final Thought:
Question 7
Which of the below codes will give the output in this format – "5:13 PM"?
Note: dateVal is an instance of new Date().
a)
b)
c)
d)
<div class="card card-block">
<h4 class="card-title">Date</h4>
<div class="card-text">
<p>{{ dateVal | date: 'Time' }}</p>
</div>
</div>
MCQ (Q7):
Which code will format dateVal to output like "5:13 PM"?
Options:
A) dateVal | date: 'fullTime'
B) dateVal | date: 'time'
C) dateVal | date: 'shortTime'
D) dateVal | date: 'Time'
Correct Answer:
C) dateVal | date: 'shortTime'
Detailed Explanation:
So, if you want something like "5:13 PM", you must use 'shortTime'!
Pro Tip:
If you ever forget, just think:
"shortTime = short output = only hours & minutes"
Simple memory trick!
Final Thought:
<ng-template #elseBlock>
<p>False block</p>
</ng-template>
MCQ (Q8):
What will be the output when the following is executed?
Given:
• showMsgDiv = false
<ng-template #elseBlock>
<p>False block</p>
</ng-template>
Options:
A) True block False block
B) True block
C) False block
D) Error stating elseBlock is not a known element of div tag
Correct Answer:
C) False block
Detailed Explanation:
Directive/Concept Meaning
*ngIf="condition" Shows element only if condition is true
else block Alternate block to render if condition is false
<ng-template> Defines a template that is not rendered unless explicitly called
ng-template is special — it’s like an invisible container which Angular only renders
when asked!
Final Thought:
Question 9
Consider the following code snippet:
constructor() { }
ngOnInit() { }
}
<div *ngIf="emp1">
Inside first div element
</div>
<div *ngIf="emp2">
Inside second div element
</div>
<div *ngIf="emp3">
Inside third div element
</div>
a) Inside first div element
Inside second div element
Inside third div element
d) Compilation error
MCQ (Q9):
Given the Angular component and template code, what will be the output?
constructor() { }
ngOnInit() { }
}
HTML:
<div *ngIf="emp1">
Inside first div element
</div>
<div *ngIf="emp2">
Inside second div element
</div>
<div *ngIf="emp3">
Inside third div element
</div>
Options:
A) Inside first div element
Inside second div element
Inside third div element
Correct Answer:
C) Inside first div element
Inside third div element
Detailed Explanation:
Summary:
• A)
➔ Wrong!
➔ All three won't render because emp2 is undefined.
• B)
➔ Wrong!
➔ emp2 still won't render, but missing third div here.
• D)
➔ Wrong!
➔ No compilation error.
➔ undefined is allowed at runtime for objects — Angular simply hides the div if
condition is falsy.
Related Concepts (Must Know!):
In templates, undefined variables just don't render the block quietly — no crash or
error!
Pro Tip:
In Angular templates, when using *ngIf, always initialize your objects or you might get
surprising empty screens!
Final Thought:
• Output = "Inside first div element" and "Inside third div element"
• Correct option: C
Question 10
State True or False.
a) True
b) False
MCQ (Q10):
State True or False:
<router-outlet></router-outlet> is one of the router directives provided by
RouterModule, which specifies where the views have to be rendered.
Options:
A) True
B) False
Correct Answer:
A) True
Detailed Explanation:
• In Angular, when you use routing, you define different components to load based
on the URL.
• <router-outlet> is a special Angular directive provided by the RouterModule.
• It acts like a placeholder (container) where the router dynamically loads the
component based on the current route.
• Without <router-outlet>, the router wouldn't know where to display the routed
component on the page!
So YES,
<router-outlet> is absolutely provided by RouterModule.
It tells Angular where to inject/render the routed views.
You can have multiple <router-outlet> in the same page for advanced routing (like
nested routes)!
Pro Tip:
Think of <router-outlet> like a theater stage :
• Different plays (components) perform on the same stage (outlet) depending on the
audience (URL).
Final Thought:
• Statement is TRUE
• Correct answer = A) True
Question 11
Consider the below component model:
email: string;
phone: number;
onSubmit() {
console.log(this.email, this.phone);
}
What should be the code in the view so that it creates a form as shown below?
The data entered by the user should be displayed on the console when the form
is submitted.
a)
<form (ngSubmit)="onSubmit()">
Email:<input type="email" [(ngModel)]="email" name="txtEmail">
<br>
Phone:<input type="number" required [(ngModel)]="phone"
name="txtPhone">
<br>
<button type="submit">Submit</button>
</form>
b)
<form #frm="ngForm" (ngSubmit)="onSubmit()">
Email:<input type="email" [(ngModel)]="email" name="txtEmail"
#em="ngModel">
<br>
Phone:<input type="number" required [(ngModel)]="phone"
name="txtPhone" #ph="ngModel">
<br>
<button type="submit">Submit</button>
</form>
c)
<form>
Email:<input type="email" [(ngModel)]="email">
<br>
Phone:<input type="number" required [(ngModel)]="phone">
<br>
<button type="submit" (submit)="onSubmit()">Submit</button>
</form>
d)
<form>
Email:<input type="email" [(ngModel)]="email">
<br>
Phone:<input type="number" required [(ngModel)]="phone">
<br>
<button type="submit" (submit)="onSubmit()">Submit</button>
</form>
a) Only A
b) Only B
c) A and B
d) A, B and C
email: string;
phone: number;
onSubmit() {
console.log(this.email, this.phone);
}
Detailed Explanation:
Option A Analysis:
<form (ngSubmit)="onSubmit()">
Email:<input type="email" [(ngModel)]="email" name="txtEmail">
<br>
Phone:<input type="number" required [(ngModel)]="phone" name="txtPhone">
<br>
<button type="submit">Submit</button>
</form>
Option B Analysis:
• Option C Analysis:
<form>
Email:<input type="email" [(ngModel)]="email">
<br>
Phone:<input type="number" required [(ngModel)]="phone">
<br>
<button type="submit" (submit)="onSubmit()">Submit</button>
</form>
• Problem 1: Inputs are missing the name attribute — ngModel needs a name.
• Problem 2: (submit)="onSubmit()" wrongly placed on button instead of form!
You can safely add #frm and #em even if you are not doing validation.
Pro Tip:
In exams, quickly check: [(ngModel)] present?
name attribute there?
(ngSubmit) on <form>?
If yes, the form setup is correct!
Final Thought:
a) CheckComponent
b) CommonComponent
c) HomeComponent
d) Compilation error
MCQ (Q12):
Given the following routes configuration:
Options:
A) CheckComponent
B) CommonComponent
C) HomeComponent
D) Compilation error
Correct Answer:
A) CheckComponent
Detailed Explanation:
• User accesses /empty ➔
➔ The router checks if there’s a match.
• { path: 'empty', redirectTo: '', pathMatch: 'full' }
o Found! empty path redirects to '' (empty path).
o pathMatch: 'full' means it only matches if the full URL is /empty, which
it is.
• So Angular now navigates to '' (empty path).
• Next, { path: '', component: CheckComponent }
o Empty path ('') is mapped to CheckComponent
• Thus CheckComponent is displayed!
Quick Flow :
/empty
➔ Redirects to / (empty path)
➔ Empty path loads ➔ CheckComponent
• B) CommonComponent →
➔ ** (wildcard) is used only if no other routes match.
➔ Here, a route matches (empty ➔ redirect to ''), so wildcard is not triggered.
• C) HomeComponent →
➔ home path is only triggered if URL is /home, not /empty.
• D) Compilation error →
➔ No syntax or configuration error here.
➔ The routes are correctly defined.
Pro Tip:
When you see a redirectTo pointing to '',
it means "go home" (default route component)!
Final Thought:
Question 13
Observe the given code and choose the correct option:
MCQ (Q13):
Given the code:
Question:
What actually happens when this getReviews() method is called?
Options:
A) The HttpClient get() method converts the synchronously received Observable array of
type IRating to JSON data.
B) The HttpClient get() method converts the synchronously received JSON data to an
Observable array of type IRating.
C) The HttpClient get() method converts the asynchronously received JSON data to an
Observable array of type IRating.
D) The HttpClient get() method converts the asynchronously received Observable array of
type IRating to JSON data.
Correct Answer:
C) The HttpClient get() method converts the asynchronously received JSON data to an
Observable array of type IRating.
Detailed Explanation:
Thus:
So get<IRating[]> ➔ means:
"Expect a list of IRating objects coming back from the server".
Pro Tip:
If you ever get confused, just think:
API ➔ sends JSON
Angular ➔ unwraps JSON, wraps in Observable
You ➔ subscribe to get your typed object!
Final Thought:
a) <router></router>
b) <router-output></router-output>
c) <router-outlet></router-outlet>
d) <router-display></router-display>
Let's go!
This Question 14 is super important because it checks your basics on Angular Routing and
Component Rendering!
MCQ (Q14):
In Angular 9 routing, which tag is used to render the matched component based on the
active route?
Options:
A) <router></router>
B) <router-output></router-output>
C) <router-outlet></router-outlet>
D) <router-display></router-display>
Correct Answer:
C) <router-outlet></router-outlet>
Detailed Explanation:
• In Angular, when you navigate to a route (like /home, /about), the router needs a
placeholder to inject the matching component dynamically onto the page.
• That placeholder is ➔ <router-outlet>!
• <router-outlet> is a special Angular directive provided by RouterModule.
• Angular will look at the URL, find the matching route, and then render the correct
component inside <router-outlet>.
So, without <router-outlet>, Angular wouldn’t know where to place the routed
components!
Why Other Options Are Wrong:
• A) <router> → Wrong!
➔ Not an Angular element. No such thing as <router> tag.
• B) <router-output> → Wrong!
➔ Looks close, but wrong keyword. It's outlet, not output.
• D) <router-display> → Wrong!
➔ Sounds logical, but Angular doesn't have this tag either.
In fact, you can have multiple <router-outlet> for nested routes too!
Pro Tip:
Think of <router-outlet> like a theater stage —
• Different plays (components) perform on the same stage depending on the audience
request (URL route).
Final Thought:
a) ngIf
b) ngFor
c) ngModel
d) ngOnInit
MCQ (Q15):
Which of the following keywords is NOT a directive?
Options:
A) ngIf
B) ngFor
C) ngModel
D) ngOnInit
Correct Answer:
D) ngOnInit
Detailed Explanation:
• A) ngIf →
➔ Structural directive ➔ Shows/hides elements based on a condition.
➔ Example:
<div *ngIf="showContent">Hello!</div>
• B) ngFor →
➔ Structural directive ➔ Used for looping over arrays/collections.
➔ Example:
• C) ngModel →
➔ Attribute directive ➔ Enables two-way data binding between input elements
and component properties.
➔ Example:
<input [(ngModel)]="userName">
• D) ngOnInit →
➔ NOT a directive!
➔ It is a lifecycle hook interface in Angular. ➔ Used to write logic that should run
when a component is initialized.
➔ Example (in component class, NOT in HTML):
Pro Tip:
If it starts with * or is used inside HTML ➔ it's almost certainly a directive!
If it's a method inside a component class ➔ it's a lifecycle hook!
Final Thought:
• Correct answer = D) ngOnInit
• ngIf, ngFor, ngModel are directives.
Question 16
Jill wants to display the price as "USD 2,021.00" in her Angular project.
Which of the following pipe should she choose?
MCQ (Q16):
Jill wants to display the price as "USD 2,021.00" in her Angular project.
Which pipe syntax should she choose?
Options:
A) {{ 2021 | currency : 'USD' : 'code' }}
B) {{ 2021 | currency : 'USD' : true }}
C) {{ 2021 | currency : 'USD' : 'String' }}
D) {{ 2021 | currency : 'USD' : 'Dollars' }}
Correct Answer:
A) {{ 2021 | currency : 'USD' : 'code' }}
Detailed Explanation:
• She wants "USD 2,021.00" — notice USD (currency code) before the amount.
• Thus ➔ display = 'code'
Therefore:
Matches perfectly!
Parameter Meaning
'symbol' Default, displays currency symbol ($, ₹, €)
'code' Displays 3-letter ISO code (USD, INR, EUR)
'symbol-narrow' Displays narrow version of the symbol (if available)
digitsInfo Optional format to control decimal places
You can also control number format like "1.0-2" (min 1 integer, min 0 fraction, max 2
fractions).
Final Thought:
Question 17
Robert has written the below code for routing. Predict the output by
considering all are valid components of the application.
MCQ (Q17):
Robert's routing code:
Options:
A) AddCustomerComponent will load when addTable is given in URL
B) AddCustodianComponent will load when addTable is given in URL
C) GetTableDetailsComponent will load by default
D) The code will throw error since we cannot give same routing name for more than one
component
Correct Answer:
B) AddCustodianComponent will load when addTable is given in URL
Detailed Explanation:
• In Angular routes configuration, if two routes have the same path, the last one
wins!
• Angular will override the earlier route with the later one silently — no compilation
error .
• It simply keeps the last matching route definition in memory at runtime.
No error will occur during compilation. Angular picks the last defined component.
Always avoid duplicate paths ➔ it’s confusing and bad practice, but Angular won't stop
you!
• In Angular routing, the last route with same path overrides previous ones.
• No compilation error happens.
• Be careful when designing route paths to avoid confusion!
Pro Tip:
In real projects, use unique paths or child routes to organize components properly!
Duplicate paths silently overriding can cause major debugging nightmares .
Final Thought:
Question 18
Sam has created two component classes called FirstComponent and
SecondComponent and he wants to make them available to the entire
module. In which of the following properties of NgModule should he declare
these two classes?
Options:
A) imports: [FirstComponent, SecondComponent]
B) declarations: [FirstComponent, SecondComponent]
C) bootstrap: [FirstComponent, SecondComponent]
D) providers: [FirstComponent, SecondComponent]
Correct Answer:
B) declarations: [FirstComponent, SecondComponent]
Detailed Explanation:
@NgModule({
declarations: [FirstComponent, SecondComponent],
...
})
• A) imports →
➔ Used to import other modules (not components, directives, or pipes).
➔ Example: BrowserModule, FormsModule, HttpClientModule, etc.
• C) bootstrap →
➔ Used to launch the main/root component (typically just one, like
AppComponent). ➔ Only the starting component goes here, not multiple normal
components.
• D) providers →
➔ Used to register services (singleton classes) for dependency injection, not
components.
Related Concepts (Must Know!):
Remember:
• Components ➔ go to declarations
• Modules ➔ go to imports
• Services ➔ go to providers
• Startup component ➔ goes to bootstrap
Pro Tip:
If it's something you use in HTML templates (components, pipes, directives) ➔ always
think declarations!
Final Thought:
Question 19
If the array is:
boatArr = [
{ boatId: 1, boatName: 'Boat1', boatRating: 30 },
{ boatId: 2, boatName: 'Boat2', boatRating: 32 },
{ boatId: 3, boatName: 'Boat3', boatRating: 34 },
{ boatId: 4, boatName: 'Boat4', boatRating: 37 }
];
a)
<table>
<tr *ngFor="let boat of boatArr">
<td>{{boat.boatId}}</td>
<td *ngIf="boat.boatRating>35">{{boat.boatName}}</td>
<td>{{boat.boatRating}}</td>
</tr>
</table>
b)
<table>
<tr *ngIf="boat.boatRating>35" *ngFor="let boat of boatArr">
<td>{{boat.boatId}}</td>
<td>{{boat.boatName}}</td>
<td>{{boat.boatRating}}</td>
</tr>
</table>
c)
<table>
<tr *ngFor="let boat of boatArr">
<td>{{boat.boatId}}</td>
<td>{{boat.boatName}}</td>
<td *ngIf="boat.boatRating>35">{{boat.boatRating}}</td>
</tr>
</table>
Options:
a) A and B
b) A Only
c) A and C
d) B Only
MCQ (Q19):
Given array:
boatArr = [
{ boatId: 1, boatName: 'Boat1', boatRating: 30 },
{ boatId: 2, boatName: 'Boat2', boatRating: 32 },
{ boatId: 3, boatName: 'Boat3', boatRating: 34 },
{ boatId: 4, boatName: 'Boat4', boatRating: 37 }
];
Required Output:
Notice:
Correct Answer:
B) A Only
Option A:
<table>
<tr *ngFor="let boat of boatArr">
<td>{{boat.boatId}}</td>
<td *ngIf="boat.boatRating>35">{{boat.boatName}}</td>
<td>{{boat.boatRating}}</td>
</tr>
</table>
Correct because:
• In the rows where boatRating ≤ 35, the second <td> (boatName) won't exist ➔ table
might be misaligned visually unless you manage missing <td> carefully.
But for this MCQ, this is the closest and correct approach.
Option B:
<table>
<tr *ngIf="boat.boatRating>35" *ngFor="let boat of boatArr">
<td>{{boat.boatId}}</td>
<td>{{boat.boatName}}</td>
<td>{{boat.boatRating}}</td>
</tr>
</table>
Wrong!
• Error: Multiple structural directives (*ngIf and *ngFor) on the same element ➔
Angular does not allow this .
• Also, even if allowed:
➔ It would render only boats where boatRating > 35, i.e., only Boat4 shown ➔
Not what we want.
Option C:
<table>
<tr *ngFor="let boat of boatArr">
<td>{{boat.boatId}}</td>
<td>{{boat.boatName}}</td>
<td *ngIf="boat.boatRating>35">{{boat.boatRating}}</td>
</tr>
</table>
Wrong!
Important Rule:
➔ Never put both *ngIf and *ngFor on same element directly.
➔ Use <ng-container> if needed for such combinations.
• *ngFor for looping, *ngIf inside loop for conditionally showing cells.
• Structural directives like *ngIf and *ngFor can't both exist on same tag (use <ng-
container> if needed).
• If a <td> is conditionally removed, be careful about table alignment!
Pro Tip:
When designing tables with dynamic columns, always check if your rows might "shift" due
to missing <td>. If needed, you can render a blank <td> when the condition fails to maintain
proper structure.
Final Thought:
Question 20
State True or False.
a) True
b) False
MCQ (Q20):
State True or False:
Both template and templateUrl must be specified in the metadata to configure the
template of the corresponding component.
Options:
A) True
B) False
Correct Answer:
B) False
Detailed Explanation:
Correct examples:
@Component({
selector: 'app-example',
template: `<h1>Hello from inline template!</h1>`
})
export class ExampleComponent { }
@Component({
selector: 'app-example',
templateUrl: './example.component.html'
})
export class ExampleComponent { }
Incorrect (Error) Example:
@Component({
selector: 'app-example',
template: `<h1>Hello</h1>`,
templateUrl: './example.component.html'
})
export class ExampleComponent { }
Best Practice:
Pro Tip:
Think like this:
"One way to talk, either inline or external, but not both together!"
Final Thought: