Most Common Angular Interview Questions :
Client Side Framework based on node js and typescript
- What are the building blocks of Angular ?
Component , Template , Service , Dependency Injection , Data binding , Routing , Module
- How does Angular Application starts ?
main.ts -> platformBrowserDynamic().bootstrapModule(AppModule) -> AppModule
Basic building block , it has template which is loaded on using the selector .
it is the html part of component .
Two types are there
1. within the component.ts enclosed in back tick
2. in a separate html file .
- How to style a component ?
Three ways : 1. inside the component.ts using style
2. in a separate css using styleUrl
3 . multiple css files can be included using styles [] .
- What is providers for component ?
It specifies the Services to be used in the component .
- Explain model to view binding ?
Two ways : {{}} Interpolation : variables defined in component can be used in template .
[] property binding : a known property of html element is bound to a variable defined in component .
- Explain view to model binding ?
using () : event binding : binds an event to method defined in component .
- Explain two way binding ?
using [()] : it is a combination of event binding and property binding , it synchronizes the data between model and view .
eg : <input [(ngModel)] = "myInput" > <span >you entered {{myInput}} </span>
Module groups related components , services , directives and pipes .
4 Parts :
1. declarations [] : list of components , pipes and directives
2. imports [] : list of modules , used to import routes
3. providers [] : list of services
4. bootstrap : the root component that Angular creates and inserts into the index.html.
The module loaded on start of the app . This imports BrowserModule . Only one per app .
Add to the DOM .
3 Types :
Components :directives with template.
Structural : add / remove DOM elements. *ng-if , *ng-for
Attribute : change the appearance or behavior of an element, component, or another directive. Eg : ngClass
- How to create custom attribute directive ?
using @Directive , in the constructor we can access the native element from ElementRef.nativeElement , using which we can manipulate the DOM.
we can also use @Input to get the input in the specified property .
- How to create custom structural directive ?
using @Directive , inject ViewContainerRef and TemplateRef , in ngOnChanges() using this.container.createEmbeddedView .
- How to pass data from parent and child component ?
Using @Input on a property in child component and pass the data to property in while it is called in parent component .
- How to pass data from child to parent component ?
- Using @ViewChild(Component class) add to a variable of Child component in parent component
- Using @Output and EventEmitter .eg : @Output() voted = new EventEmitter<boolean>(); this.voted.emit(agreed);
Note: EventEmitter is subclass of Observable .
- How to pass data between unrelated component ?
Using a service , create a variable as BehaviorSubject type , another variable as observable for the variable and a method to change the value . Subscribe the observable in both components , if value is changed from one component it will be reflected in other component .
For a component Angular creates it, renders it, creates and renders its children, checks it when its data-bound properties change, and destroys it before removing it from the DOM.
- ngOnChanges()
- ngOnInit()
- ngDoCheck()
- ngAfterContentInit()
- ngAfterContentChecked()
- ngAfterViewInit()
- ngAfterViewChecked()
- ngOnDestroy()
Before these methods the constructor is called .
It is used to perform a task , like http put or get .
- What is dependency Injection ?
Angular creates instance of all the services and makes it available for the component . Services are singleton .
- constructor injection :dependencies are made available through constructor
- property injection :directly setting the object property .
For optional dependencies , injector will inject null if dependencies are not found.
resolves token into dependency .
- Explain Hierarchical injector.
tree of injectors that parallels an app's component tree
is an instruction to the DI system on how to obtain a value for a dependency .
does not allow content be injected in the projected content
- How to create a service ?
using @Injectible
- When to use @providedin : root ?
when service is used anyway , if service is rarely used specify the service in provider [] of component .providedIn can also hold any module other than root .
Loading of components based on URL .
- How to add routing to an angular app?
Create Routes[] in module , in imports add RouterModule.forRoot(appRoutes)
.Add router-outlet where the component should be loaded.
- What is RouterLink directive ?
generates hyperlink that can be embedded in anchor tags for navigation .
It is used to match a wildcard and redirect to a different URL . The component associated with the redirected URL will be loaded .
enables hash(#) based routing .
AppRoutingModule is top level module dedicated for routing .In the ng module exports: [ RouterModule ]
- What is a feature module ?
Contains a group of components, directives pipes with similar features and imported in root module .
Root module should be split into feature module and a routing module . Feature modules can have its own routing module . RouterModule.forRoot() is used in the app Routing Module and RouterModule.forChild() is used in the Routing Module of feature module . In the app routing module loadChildren is used instead of component .
tells the router whether or not to navigate to the requested route
CanActivate
CanActivateChild
CanDeactivate
CanLoad
Handles user data . 2 Types :
- Template driven :
- Model Driven /Reactive
- Explain Template driven form .
Used for the following :
- two-way data binding : ngModel , ngModelGroup
- change tracking: pristine, dirty , valid
- validation : required , pattern , minlength
- error handling *ngIf
- Explain Model Driven / reactive Forms .
Using ReactiveFormsModule
1. FormGroup FormControl
2. FormBuilder
3. FormArray
- How to create a custom validator ?
Create a typescript class with a method
Add it in formbuilder . Group method for the name
provides hidden area on the page for scripts HTML and CSS . Markup and styles in this area will not affect the other part of the page also will not be affected by other part .
- What is view encapsulation ?
Added to a component using encapsulation attribute .
- ViewEncapsulation.Native : lets angular to use the shadow DOM . isolates the component , default styles will not be applied to this component .
- ViewEncapsulation.Emulated : applied by default
- ViewEncapsulation.None : the styles defined for this component is applied to all other component .
- Diffentiate observable and promise.
- Promise is executed immediately, observable is lazy and executes only if it is subscribed.
- Observable can be cancelled, promise can't.
- Promise handles single event , observable is a stream and allows multiple events.
It allows to bind with the observable directly in the template.
The advantage of using AsyncPipe is that we don’t need to call subscribe on our observable and store the intermediate data on our component.
- How to create a promise .
Promise handles single event when async operation completes or fails
let promise = new Promise((resolve, reject) => {
//http call
});
return promise;
Convert from one type of source code to another type of source code .
- Build time : takes the type script files and compiles it to javascript files
- Runtime : happens at the browser at runtime . Typescript compiles is loaded in the browser which compiles the typescript files on the fly .
A tool to bundle application source code and load that code from server to browser .
- What are the special characters in angular ?
- [] : property binding
- {{}} : interpolation
- () : event binding
- [()] : two way binding
- | : pipe
- * : structural directive
- ? : safe navigation
- # : template reference variable
- Explain folders in angular project ?
- e2e : contain end to end tests
- src : application code
- assets : static content like images .
- environments : configuration for developer and production environments .
- Explain the important files in Angular .
- package.json
- angular.json
- main.ts
- tslint.json
- tsconfig.app.json
- proxyconfig.json
- How do you change the path of node_modules?
- What are the symbols ~ and ^ in node modules
- How do you build your application for production
ng build --prod
the bundles will be generated in dist folder
Polyfil scripts should be loaded for angular application to work on the supported browser .
- Explain angular binding Construct .
- Interpolation
- property binding
- attribute binding
- class binding
- style binding
- event binding
XML Http Request built in object in browser to request data from server .
- How does angular safegaurd the app against XSS attacks ?
Cross side scripting is injecting malicious code in web pages gain access and steal data .Sanitization : Inspection of untrusted value and turing it value that is safe to insert in DOM. Security Constructs are as below
- HTML
- style
- URL
- resource URL
helps preventing the cross side .
- Sanitize :
- bypassSecurityTrustHtml
- bypassSecurityTrustScript
- bypassSecurityTrustStyle
- bypassSecurityTrustUrl
- bypassSecurityTrustResourceUrl
- How does hot reloading works internally?