Which decorator is used to declare a class as an Angular component?

  • @Component
  • @Directive
  • @Injectable
  • @NgModule
The @Component decorator is used to declare a class as an Angular component. Components are the fundamental building blocks of Angular applications, representing user interface elements. NgModule is used to define modules, @Directive is used for custom directives, and @Injectable is used for dependency injection, but they are not used to declare components.

When you want to add a specific Angular capability to your project (like PWA support), you would use the ng ________ command.

  • add
  • enable
  • generate
  • install
To add a specific Angular capability to your project, like Progressive Web App (PWA) support, you would use the ng enable command. This command allows you to enable various features and capabilities in your Angular application.

When implementing a custom async validator, the returned observable should emit null for valid inputs and an error object for invalid inputs. This error object typically has a key that describes the error and a ______ value.

  • boolean
  • description
  • message
  • string
When implementing a custom async validator in Angular, the error object typically has a key that describes the error (e.g., 'customError') and a message value (a string) that provides details about the specific validation error. This message is useful for displaying error messages to users when their input is invalid.

How can you implement custom validators in a template-driven form?

  • Custom validation can't be implemented in template-driven forms.
  • Define custom validation functions in the component.
  • Use the Validators class methods within the form template.
  • Utilize the ReactiveFormsModule for template-driven forms.
In template-driven forms, custom validators are implemented by defining custom validation functions in the component. These functions can be used to validate form controls by adding them to the Validators array in the HTML template. While ReactiveForms use Validators directly in the form template, template-driven forms require custom logic in the component to implement custom validation.

What is the purpose of the [(ngModel)] syntax in template-driven forms?

  • To create a two-way data binding for forms.
  • To define CSS styles for form elements.
  • To define form validation rules.
  • To set the form's action attribute for submission.
In template-driven forms in Angular, [(ngModel)] is used to create a two-way data binding between form controls in the template and the corresponding data model in the component class. This allows changes in the form controls to automatically update the model, and changes in the model to update the form controls. It's a key feature for working with form data.

Which directive is used to create a custom attribute in Angular?

  • @Attribute
  • @CustomAttribute
  • @Decorator
  • @Directive
In Angular, the @Directive decorator is used to create custom attributes. By defining a directive with this decorator, you can attach custom behavior or properties to elements in your Angular templates, creating custom attributes.

To ensure that Angular recognizes your custom component's value accessor to interact with the ngModel directive, you need to add it to the providers array of your component and associate it with the ______ token.

  • ControlValueAccessor
  • FormControl
  • NGModel
  • NGValueAccessor
To make Angular recognize your custom component's value accessor for ngModel, you need to add it to the providers array of your component and associate it with the ControlValueAccessor token. This token signifies that your custom component implements the ControlValueAccessor interface, which is necessary for two-way data binding with ngModel.

Which of the following is a push-based mechanism in JavaScript?

  • Callbacks
  • Observables
  • Promises
  • Synchronous functions
Observables are a push-based mechanism in JavaScript. They allow you to work with asynchronous data streams, where data can be emitted at any time, rather than being pulled when needed as with Promises or using callbacks.

You are tasked with creating a directive that changes the background color of an element when it's hovered over. Which type of directive would be best suited for this task?

  • Attribute Directive
  • Component Directive
  • Service Directive
  • Structural Directive
For changing the background color of an element when hovered, an Attribute Directive is the best choice. Attribute Directives are used to modify the behavior or appearance of an element. You can create an Attribute Directive that listens for the mouseenter and mouseleave events and modifies the element's style accordingly. This approach is more suitable than other types of directives for this specific task.

How can you pass data from a parent component to a child component in Angular?

  • EventEmitter
  • Input property
  • Output property
  • ViewChild
In Angular, you can pass data from a parent component to a child component by using an input property. Input properties allow you to bind a value in the parent component to a property in the child component, enabling data transfer from parent to child.