General Knowledge Flashcards

1
Q

How does ng-Content differ from string Interpolation {{myValue}}?

Show Example

A

String interpolation comes from the myValue in the current component while ng-Content comes from the parent component.

<xxx> <p>Mary Had a Little Lamb</p>
<br></br> <div>Commercial Break</div> <xxx></xxx></xxx>

Where xxx is defined as:

heading goes here

<ng-content></ng-content>

<—- this is the p and div from above

footer goes here

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

List the 8 life-cycle hooks in order.

A
  1. ngOnChanges()
  2. ngOnInit()
  3. ngDoCheck()
  4. ngAfterContentInit() - after Angular projects external content into the component’s view
  5. ngAfterContentChecked() - after the ngAfterContentInit() and every subsequent ngDoCheck()
  6. ngAfterViewInit() -Called once after the first ngAfterContentChecked()
  7. ngAfterViewChecked() - Called after the ngAfterViewInit and every subsequent ngAfterContentChecked()
  8. ngOnDestroy()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What module in AppModule contains all the base functionality that is required by Angular?

A

BrowserModule

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

Show how to make a Component selector (‘app-server’) be an attribute type.

Show it in use.

A

selector: ‘[app-server]’

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

Show how to make a Component selector (‘app-server’) be an class.

Show it in use.

A

selector: ‘.app-server’

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

Explain the use of square brackets in HTML.

A

Applied to an attribute that is already valid for the selector,
indicates the value of that property will be bound to the expression after the equal sign.

Otherwise, if the attribute is NOT a part of the selector,

it still binds to a variable, but here it is a directive.

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

Show how to change what appears between the opening and closing tags of an element such as in

(See image)

A

(see image)

innerText is a native DOM element.
allowNewServer is a boolean which will be converted to string

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

Show the attributes that are native DOM vs Angular for triggering a click event.

A

native: onclick=””
Angular: (click)=””

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

Is this valid: (click)=”status = true ? ‘okay’ : ‘not okay’ “

A

yes, it can work with expressions.

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

What is the attribute for the DOM input element that will trigger every time a character is entered or removed.

A

(input)=” ourFunction( $event ) “

ourFunction(event: Event ) { // or event: any
this.serverName = (event.target).value;

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

What must be added to the App NgModule Imports in order to use two-way event binding with [(ngModel)] ?

A

import FormsModule which is from ‘@angular/forms’

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

What is the syntax of the directive selector field?

A

selector: “ [myDirective] “

square brackets

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

what indicates a “local reference” to an element in HTML?

A

Starts with hash sign #

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

Show the syntax of ngIf with else.

A

See attached image

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

Show the format of ngStyle directive.

A

(See Attached Image)

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

Show use of ngClass to conditionally add classes.

A

(See Attached Image)

17
Q

Show using ngFor

A

(See Attached Image)

18
Q

Difference between ngFor .. of versus ngFor … in?

A

‘of’ is used to iterate over iteratable like Arrays or strings
ex: let names = [‘Gertrude’, ‘Henry’, ‘Melvin’, ‘Billy Bob’];
for (let animal of animals) { // Random name for our animal
let nameIdx = Math.floor(Math.random() * names.length); console.log(${names[nameIdx]} the ${animal});
}

OR
 let str = 'abcde';

for (let char of str) {
console.log(char.toUpperCase().repeat(3));
}

‘in’ is used to iterate over properties of an object.

ex: let oldCar = { make: 'Toyota', model: 'Tercel', year: '1996' };
 for (let key in oldCar) {
 console.log(`${key} --\> ${oldCar[key]}`);
 }
19
Q

Show using ngFor in such a way as to get the index of each element.

A

*ngFor=”let logItem of logs; let i = index”

20
Q

How do you declare a property in a component that will be accessible from a parent.

For example: in the parent HTML file we have:

<thechild> ... </thechild>

And the child is:

export class myChildComponent … {

}

A

import { … Input } from ‘@angular/core’;

export class myChildComponent {
 @Input( ) useColor**:** string;
21
Q

How to decorate @Input or @Output so it has an alias from the parent … like:

Where to place the alias in: @Input( ) server: string;

A

@Input( ‘newServer’ ) server: string;

newServer becomes the alias for server

likewise
@Output( ‘replaceWith’ ) internalname = new EventEmitter …

22
Q

How can a component notify a calling parent component of event(s).

A

The Parent:

<thechild> ....</thechild>

The Child:
public export theChildComponent
@Output( ) targetChanged = new EventEmitter<newtarg : string>( ) <strong>;</strong></newtarg>

23
Q

Angular enforces encapsulation of CSS files; each CSS that is a part of a component will only affect that component’s HTML.

TRUE or FALSE

A

TRUE

24
Q

How can you override the fact that Angular does View Encapsulation of CSS styles so they won’t affect other components.

@Component( {
selector:
template:
style:

And: is this a good practice?

A

@Component( {
selector:
template:
​style:
encapsulation: ViewEncapsulation.None

And: is this a good practice? No

25
Q

Describe the effect of the three settings for ViewEncapsulation.

A

None - NO style encapulation; NO shadow DOM
Emulated (default) - style encapulation; NO shadow DOM
Native - style encapulation; shadow DOM

26
Q

Show example of @ViewChild ( Angular >= v8 )

A

Parent:
@ViewChild( ‘headingTag’, {static: true} ) header: ElementRef;
ngOnInit( ) {
console.log( this.headingTag.nativeElement.textContent);

static: true allows it to have effect on ngOnInit, otherwise false waits until afterviewinit
- —————————————————————————–

Child component:

{{ name }}

27
Q

Since @ViewChild cannot look inside ng-content in the child, show an example of how to do that.

A

Parent:
@ContentChild( ‘ contentDivTag’ , {static: true }) dv: ElementRef ;

header

<ngcontent></ngcontent>

footer

console. log(‘ dv ‘ + dv.nativeElement.textContent;
- ————————————————————–
static: true allows it to be used in ngOnInit, otherwise it won’t be available until AfterContentInit

Child:

<app-parent><br></br> <div> something goes here </div></app-parent>

28
Q

What is the best way to access an element in a custom directive - NOT using ElementRef directly?

A

Renderer2
import { Diirective, OnInit, ElementRef, Renderer2 } .. ‘@angular/core’
constructor(
private elRef: ElementRef,
private renderer: Renderer2 )
ngOnInit() {
this.renderer.setStyle( this.elRef.nativeElement ,
‘background-color’ ,‘blue’ );

29
Q

Show an example of using HostListener.

A

import { … HostListener } from @angular/core;

used inside a directive for example:

@HostListener( mouseenter ) mouseover( eventData: Event ) {
this.renderer.setStyle( this.elRef.nativeElement ,
‘background-color’ , ‘blue’, false false );
}

@HostListener( mouseleave ) mouseover( eventData: Event ) {
this.renderer.setStyle( this.elRef.nativeElement ,
‘background-color’ , ‘transparent’ , false, false);
}

30
Q

Show use of HostBinding.

A

import { HostBinding, … } from @angular/core ;

export class myDirective implements OnInit {
 @HostBinding( ' style.backgroundColor ' ) backgroundColor: string = **'transparent';**

@HostListener( ‘mouseenter’ ) mouseover( eventData: Event ) {
this.backgroundColor = ‘blue’;
}

@HostListener( ‘mouseleave’ ) mouseover( eventData: Event ) {
this.backgroundColor = ‘transparent’;

}