What is the primary difference between Observables and Promises?
- Observables are only used for HTTP requests, while Promises are for other asynchronous tasks
- Observables can handle multiple values over time, while Promises handle a single value
- Promises are asynchronous, while Observables are synchronous
- Promises are part of JavaScript, while Observables are part of Angular
The primary difference is that Observables can handle multiple values over time, while Promises handle a single value. Observables are often used for handling streams of data, such as events, real-time updates, or user input. Promises are better suited for handling a single asynchronous operation.
What should you do to ensure that an HTTP request is unsubscribed automatically when a component is destroyed?
- Automatically unsubscribed by default
- Manually unsubscribe in the ngOnDestroy lifecycle hook
- Use the async pipe
- Use the takeUntil() operator with a subject
To ensure that an HTTP request is unsubscribed automatically when a component is destroyed, you should use the 'takeUntil()' operator with a subject. This pattern helps prevent memory leaks by unsubscribing from observable streams when the component is no longer in use.
Which testing framework is primarily used alongside Angular for unit testing?
- Jasmine
- Jest
- Mocha
- Protractor
Jasmine is the most commonly used testing framework for unit testing in Angular applications. It provides a rich set of features for writing and running tests, including matchers for assertions and spies for tracking function calls.
What is the main purpose of Angular's ngIf directive?
- To create a new Angular component.
- To conditionally show or hide elements.
- To define a route in an Angular application.
- To style Angular components.
The primary purpose of Angular's ngIf directive is to conditionally show or hide elements in the DOM based on a given condition. This is useful for displaying or hiding parts of a user interface depending on the application's state or user interactions. The other options do not accurately describe the main purpose of ngIf.
To update your Angular CLI globally to the latest version, you would use the command npm install -g ________.
- @angular/cli
- angular
- ng
- typescript
To update your Angular CLI globally to the latest version, you would use the command npm install -g @angular/cli. This command installs or updates the Angular CLI package globally, ensuring you have the latest version available.
Your application's main dashboard has multiple child components displaying real-time data. To optimize performance, which strategy would you employ to ensure only relevant components are re-rendered upon data changes?
- Memoization
- Pure Components
- Dynamic Imports
- Lazy Loading
In this scenario, to ensure only relevant components are re-rendered upon data changes, you should use Pure Components. Pure Components are optimized to re-render only when their input properties change, and they don't have side effects. Memoization (Option 1) can be used for caching expensive calculations but doesn't handle re-rendering. Dynamic Imports (Option 3) and Lazy Loading (Option 4) are related to code splitting and module loading, not component rendering optimization.
Which command allows you to add a new capability, like Angular Material, to your Angular application using Angular CLI?
- ng new
- ng add
- ng generate
- ng install
To add a new capability or library to your Angular application, such as Angular Material, you can use the "ng add" command. This command not only installs the library but also performs any necessary configuration, making it a convenient way to enhance your project.
The ______ guard is used to decide if the current route can be deactivated.
- CanActivate
- CanActivateChild
- CanDeactivate
- CanLoad
In Angular, the CanDeactivate guard is used to decide if the current route can be deactivated. It's often used to prompt users for confirmation before leaving a page with unsaved changes.
When building your Angular application for production, which command ensures that Angular's AOT (Ahead of Time) compiler is used?
- ng build --ahead-of-time
- ng build --aot
- ng build --optimize
- ng build --prod
To ensure that Angular's AOT compiler is used when building your application for production, you should use the ng build --aot command. AOT compilation improves runtime performance and reduces bundle size by pre-compiling Angular templates.
You are developing an Angular application that needs to maintain a consistent state across multiple components and services. Which approach would you consider for managing this state?
- Angular Services
- BehaviorSubject
- LocalStorage
- NgRx Store
For managing a consistent state across multiple components and services in Angular, you would consider using NgRx Store. NgRx is a state management library that allows you to maintain a centralized, immutable state in your application, making it easier to manage and share state data among various parts of your app.