You notice that styles defined in a component are affecting other components throughout the application. Which view encapsulation mode might the component be using?
- None (ViewEncapsulation.None)
- Shadow DOM (ViewEncapsulation.ShadowDom)
- Emulated (ViewEncapsulation.Emulated)
- Native (ViewEncapsulation.Native)
When styles defined in a component affect other components, it's likely that the component is using the "Emulated" (ViewEncapsulation.Emulated) mode. In this mode, Angular emulates the shadow DOM, and styles are scoped to the component, but they can leak out and affect other components. The other options (None, Shadow DOM, Native) have different encapsulation behaviors.
To ensure a single instance of a service is available throughout the app, set the providedIn property to ________.
- 'app'
- 'root'
- 'shared'
- 'singleton'
To ensure a single instance of a service is available throughout the app, you should set the providedIn property to 'root'. This means that the service is provided at the application level, and there will be only one instance of the service for the entire app, making it a singleton service that can be injected into multiple components.
You are developing an Angular application where a child component needs to notify its parent component about certain events. Which mechanism would you use?
- @Input and @Output
- EventEmitter
- Subject
- ngModelChange Event
To have a child component notify its parent in Angular, you typically use the @Input and @Output decorators. The child component can send data or events to the parent component using @Output, creating a custom event emitter. EventEmitter can be used, but it's often associated with custom events. Subjects are used for more complex scenarios. The ngModelChange event is used for form input changes, not parent-child communication.
When an Observable is no longer needed, it should be ________ to prevent memory leaks.
- Composed
- Pipelined
- Subscribed
- Unsubscribed
To prevent memory leaks in Angular applications when an Observable is no longer needed, it should be unsubscribed. Failing to unsubscribe from Observables can lead to resource leaks, as they may continue to emit values and hold references to objects even after the component or service that used them is destroyed. Properly unsubscribing from Observables ensures that they are cleaned up and do not cause memory issues.
Custom attribute directives can alter the appearance or behavior of an element without changing its ________.
- CSS styles.
- HTML structure.
- JavaScript code.
- Original attributes or properties.
Custom attribute directives in Angular can alter the appearance or behavior of an element without changing its HTML structure. They can manipulate the element's behavior or appearance by binding to its properties or attributes, but they typically do not modify the fundamental structure of the HTML element. This allows for greater flexibility and reusability in Angular applications.
How can you pass data to a dynamically created component?
- Using Angular's data binding with @Input().
- By directly modifying the component's internal state.
- Through a centralized data store like Redux.
- By using Angular services and injecting data.
To pass data to a dynamically created component in Angular, you can use Angular's data binding with @Input() decorators. This allows you to bind properties of the dynamic component to the parent component's properties, enabling seamless data communication between them. The other options may be possible in certain scenarios but are not the standard approach for passing data to dynamically created components.
Your application requires a custom directive that modifies the structure of the DOM based on user roles. Which type of directive should you consider creating?
- Attribute Directive
- Component Directive
- Service Directive
- Structural Directive
When you need to modify the structure of the DOM based on user roles, a Structural Directive is the most appropriate choice. Structural Directives, such as ngIf and ngFor, can conditionally add or remove elements from the DOM, allowing you to customize the view based on user roles or conditions. This flexibility makes them well-suited for this scenario.
An Observable that can emit multiple values over time is different from a Promise, which can resolve only ________.
- multiple times
- never
- once
- twice
An Observable can emit multiple values over time, whereas a Promise can resolve only once. This fundamental difference makes Observables suitable for handling asynchronous streams of data, while Promises are used for handling single asynchronous operations.
Which decorator is used to inject a service into an Angular component?
- @Injectable()
- @Component()
- @Service()
- @InjectService()
To inject a service into an Angular component, you use the @Injectable() decorator. This decorator is applied to the service class, allowing Angular's dependency injection system to recognize and provide the service when requested by a component. The other options (@Component(), @Service(), @InjectService()) are not used for this purpose.
When using the OnPush strategy, a component will be checked if a new object reference is passed to one of its ________.
- Inputs
- Methods
- Outputs
- Variables
When using the OnPush change detection strategy in Angular, a component will be checked if a new object reference is passed to one of its inputs. This means that the component will only re-render if there's a change in the input values. The OnPush strategy is an optimization to reduce unnecessary checks and re-renders.