Consider this Angular component using a facade service to get user data.
import { Component } from '@angular/core';
import { UserFacade } from './user.facade';
@Component({
selector: 'app-user',
template: `User: {{ userName }}`
})
export class UserComponent {
userName = '';
constructor(private userFacade: UserFacade) {
this.userFacade.getUserName().subscribe(name => this.userName = name);
}
}The UserFacade returns an observable emitting 'Alice'.
What will the component display?
import { Injectable } from '@angular/core'; import { of } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class UserFacade { getUserName() { return of('Alice'); } }
Think about what the observable emits and how the subscription updates userName.
The facade service returns an observable emitting 'Alice'. The component subscribes and sets userName to 'Alice', so the template shows 'User: Alice'.
isLoading after facade call?Given this Angular facade service and component snippet:
export class DataFacade {
isLoading = false;
fetchData() {
this.isLoading = true;
setTimeout(() => this.isLoading = false, 1000);
}
}
@Component({ selector: 'app-data', template: `Loading: {{ facade.isLoading }}` })
export class DataComponent {
constructor(public facade: DataFacade) {
this.facade.fetchData();
}
}Immediately after fetchData() is called, what is the value of facade.isLoading?
Consider the synchronous and asynchronous parts of fetchData().
The method sets isLoading to true immediately, then resets it to false after 1 second asynchronously. So right after calling, it is true.
Angular 17 supports standalone components and the new inject() function.
Which code snippet correctly injects UserFacade in a standalone component?
Remember that inject() is used inside class bodies, not outside.
Option A correctly uses inject(UserFacade) inside the class body to inject the service in a standalone component. Option A uses inject() outside the class, which is invalid. Option A uses constructor injection, valid but not using inject(). Option A manually creates the service, which breaks Angular DI.
Look at this Angular facade service:
@Injectable({ providedIn: 'root' })
export class ProductFacade {
products$;
constructor(private api: ApiService) {
this.products$ = this.api.getProducts();
}
}The component subscribes to products$ but never unsubscribes. Why is this a problem?
Think about Angular subscriptions and component lifecycle.
The observable products$ is subscribed to in the component. If the component does not unsubscribe before destruction, the subscription stays active, causing a memory leak. The facade itself is fine, but the component must manage subscriptions.
Choose the best explanation of why using a facade service is helpful in Angular applications.
Think about how facades simplify component code and separate concerns.
The facade pattern provides a simple interface to complex logic like state and API calls. This keeps components clean and easier to maintain. It does not replace DI or automatically cache data.