Which module should you import to use the HttpClient in an Angular application?
- @angular/common/http
- @angular/core
- @angular/forms
- @angular/router
To use the HttpClient in an Angular application, you should import the @angular/common/http module. This module provides the HttpClient service for making HTTP requests.
A client asks for a feature where a child component should be able to inform its parent component when a button inside the child component is clicked. How would you implement this interaction?
- Use the @Output decorator to emit an event from the child component and bind to it in the parent component.
- Use the @ViewChild decorator to access the child component's method directly from the parent component.
- Use the @Input decorator to pass a callback function from the parent to the child component.
- Use the @HostListener decorator to listen for events in the child component and trigger a method in the parent component.
To implement communication from child to parent in Angular, you can use the @Output decorator to emit an event from the child component, and the parent component can bind to this event to listen for changes. This allows the child to inform the parent when the button is clicked. The other options are not the typical approach for this scenario.
In Angular's reactive forms, the method used to mark a control as touched is called ______.
- markAsDirty()
- markAsTouched()
- setDirty()
- setTouched()
In Angular's reactive forms, the method used to mark a control as touched is called markAsTouched(). This method is used to indicate that a form control has been interacted with by the user. It's commonly used for validation and to show error messages for untouched controls.
When creating a custom form control in Angular, which interface should it implement to work seamlessly with Angular's form directives?
- ControlValueAccessor
- FormControl
- FormGroup
- FormControlDirective
When creating a custom form control in Angular, it should implement the ControlValueAccessor interface to work seamlessly with Angular's form directives. This interface provides methods for connecting custom form controls with Angular's forms API, enabling the control to interact correctly with form directives like ngModel, formControl, and formGroup. The other options (FormControl, FormGroup, FormControlDirective) are related to form handling but do not specifically enable custom form control integration.
In Jasmine, which function is used to create spies that can track calls, arguments, and return values?
- createSpy
- spyOn
- trackSpy
- watchSpy
In Jasmine, the spyOn function is used to create spies. Spies are functions that can replace existing functions or be standalone, and they allow you to track function calls, arguments passed to them, and even control their behavior, including returning specific values.
Which is NOT a lifecycle hook in Angular?
- ngOnInit
- ngRender
- ngAfterViewInit
- ngOnDestroy
ngRender is not a standard lifecycle hook in Angular. The other options (ngOnInit, ngAfterViewInit, ngOnDestroy) are all valid Angular lifecycle hooks. These hooks allow developers to tap into various stages of a component's lifecycle to perform actions like initialization, cleanup, and interaction with the DOM. Understanding these hooks is crucial for managing component behavior and interactions in Angular applications.
In Jasmine, the ______ function is used to define expectations for a test.
- beforeEach
- describe
- expect
- it
In Jasmine, the expect function is used to define expectations (assertions) for a test. It is used within an it block to specify what should happen during the test and what should be verified as correct behavior.
When using , which lifecycle hook is ideal for accessing projected content inside the child component?
- ngAfterContentInit()
- ngAfterViewInit()
- ngOnChanges()
- ngOnInit()
When using , the ideal lifecycle hook for accessing projected content inside the child component is ngAfterContentInit(). This hook is triggered after content projection has taken place, allowing you to work with the projected content effectively. ngOnInit() is too early in the component lifecycle, and ngOnChanges() and ngAfterViewInit() are not specifically related to content projection.
What is the primary use of in Angular?
- To define a new component in Angular.
- To display data from an API.
- To project content from a parent component into a child component.
- To style Angular components using CSS.
The primary use of in Angular is to project content from a parent component into a child component. This allows you to pass content or HTML elements from a parent component to a designated location in a child component, facilitating component reusability and flexibility. It's a fundamental concept in Angular for creating dynamic and customizable components.
In template-driven forms, the ______ directive is used to bind an input field to a property in the component's class.
- ngBinding
- ngDirective
- ngModel
- ngTemplate
In template-driven forms in Angular, the "ngModel" directive is used to bind an input field to a property in the component's class. This directive establishes two-way data binding between the form control and the component class property, allowing changes in the input field to be reflected in the component and vice versa.