Angular Flashcards

1
Q

What are the key components of Angular?

A

1) Component - basic building block that controls HTML view

2) Module - a set of components, services, and classes that can be used within module. Modules split application into logical pieces

3) Template - These represent the views of an Angular application.

4) Services
Angular services are objects that get instantiated just once during the lifetime of an application. They contain methods that maintain data throughout the life of an application, i.e., data is available all the time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a Component?

A

Angular is a framework that consists of one or many components. So it is a basic building block. Components consists of typescript classes annotated with Component annotations, associated HTML page and CSS files.

Component class has properties and methods that allow operate on this properties. Properties are bind to Html view elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are directives?

A

Directives add behaviour to an existing DOM element or an existing component instance.

@Directive({ selector: ‘[myHighlight]’ })
export class HighlightDirective {
constructor(el: ElementRef) {
el.nativeElement.style.backgroundColor = ‘yellow’;
}
}

USAGE:

<p>Highlight me!</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the differences between Component and Directive?

A

In a short note, A component(@component) is a directive-with-a-template.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a template?

A

A template is a HTML view where you can display data by binding controls to properties of an Angular component. You can store your component’s template in one of two places. You can define it inline using the template property, or you can define the template in a separate HTML file and link to it in the component metadata using the @Component decorator’s templateUrl property.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are Modules?

A

Modules are logical boundaries in your application and the application is divided into separate modules to separate the functionality of your application.

@NgModule ({
imports: [ BrowserModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ],
providers: []
})
export class AppModule { }

The NgModule decorator has five important(among all) options

1) The imports option is used to import other dependent modules. The BrowserModule is required by default for any web-based angular application

2) The declarations option is used to define components in the respective module

3) The bootstrap option tells Angular which Component to bootstrap in the application

4) The provider’s option is used to configure a set of injectable objects that are available in the injector of this module.

5) The entry components option is a set of components dynamically loaded into the view.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between “ngOnInit” and “constructor”?

A

ngOnInit - the initialization/declaration and avoid stuff to work in the constructor

constructor - to setup Dependency Injection and not much else.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are lifecycle hooks available?

A

1) ngOnChanges - when value of the data bound property change this method is invoked

2) ngOnInit - This is called whenever the initialization of the directive/component after Angular first displays the data-bound properties happens.

3) ngOnDestroy - This is the cleanup phase just before Angular destroys the directive/component.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a data binding?

A

Data binding is a core concept in Angular and allows to define communication between a component and the DOM, making it very easy to define interactive applications without worrying about pushing and pulling data. There are four forms of data binding(divided as 3 categories) which differ in the way the data is flowing

1) From the Component to the DOM:
Interpolation: {{ value }}: Adds the value of a property from the component

<li>Name: {{ user.name }}</li>

<li>Address: {{ user.address }}</li>

2) Property binding: [property]=”value”:
The value is passed from the component to the specified property or simple HTML attribute

<input type=”email” [value]=”user.email”>

3) From the DOM to the Component: Event binding: (event)=”function”: When a specific DOM event happens (eg.: click, change, keyup), call the specified method in the component

<button (click)=”logout()”></button>

4) Two-way binding: Two-way data binding: [(ngModel)]=”value”: Two-way data binding allows to have the data flow both ways. For example, in the below code snippet, both the email DOM input and component email property are in sync

<input type=”email” [(ngModel)]=”user.email”>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is metadata?

A

Metadata is used to decorate a class so that it can configure the expected behavior of the class. The metadata is represented by decorators

[1] Class decorators, e.g. @Component and @NgModule

@Component({
selector: ‘my-component’,
template: ‘<div>Class decorator</div>’,
})
export class MyComponent {
constructor() {
console.log(‘Hey I am a component!’);
}
}

[2] Property decorators Used for properties inside classes, e.g. @Input and @Output

@Component({
selector: ‘my-component’,
template: ‘<div>Property decorator</div>’
})

[3] Method decorators Used for methods inside classes, e.g. @HostListener

@Component({
selector: ‘my-component’,
template: ‘<div>Method decorator</div>’
})
export class MyComponent {
@HostListener(‘click’, [‘$event’])
onHostClick(event: Event) {
// clicked, event available
}
}

[4] Parameter decorators Used for parameters inside class constructors, e.g. @Inject, Optional

@Component({
selector: ‘my-component’,
template: ‘<div>Parameter decorator</div>’
})
export class MyComponent {
constructor(@Inject(MyService) myService) {
console.log(myService); // MyService
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is Angular CLI?

A

Angular CLI(Command Line Interface) is a command line interface to scaffold and build angular apps using nodejs style (commonJs) modules. You need to install using below npm command,

1) Creating New Project: ng new
2) Generating Components, Directives & Services: ng generate/g The different types of commands would be,
3) Running the Project: ng serve

How well did you know this?
1
Not at all
2
3
4
5
Perfectly