FrontendDeveloper.in

Angular Interview Questions

  • Question 91

    How do you provide configuration inheritance?

    Angular Compiler supports configuration inheritance through extends in the tsconfig.json on angularCompilerOptions. i.e, The configuration from the base file(for example, tsconfig.base.json) are loaded first, then overridden by those in the inheriting config file.

    {
    "extends": "../tsconfig.base.json",
    "compilerOptions": {
    "experimentalDecorators": true,
    ...
    },
    "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "preserveWhitespaces": true,
    ...
    }
    }
    
  • Question 92

    How do you specify angular template compiler options?

    The angular template compiler options are specified as members of the angularCompilerOptions object in the tsconfig.json file. These options will be specified adjacent to typescript compiler options.

    {
    "compilerOptions": {
    "experimentalDecorators": true,
    ...
    },
    "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "preserveWhitespaces": true,
    ...
    }
    }
    
  • Question 93

    How do you enable binding expression validation?

    You can enable binding expression validation explicitly by adding the compiler option fullTemplateTypeCheck in the "angularCompilerOptions" of the project's tsconfig.json. It produces error messages when a type error is detected in a template binding expression.

    For example, consider the following component:

    @Component({
    selector: 'my-component',
    template: '{{user.contacts.email}}'
    })
    class MyComponent {
    user?: User;
    }
    

    This will produce the following error:

    my.component.ts.MyComponent.html(1,1): : Property 'contacts' does not exist on type 'User'. Did you mean 'contact'?
    
  • Question 94

    What is the purpose of any type cast function?

    You can disable binding expression type checking using $any() type cast function(by surrounding the expression). In the following example, the error Property contacts does not exist is suppressed by casting user to the any type.

    template:
    '{{ $any(user).contacts.email }}'
    

    The $any() cast function also works with this to allow access to undeclared members of the component.

    template:
    '{{ $any(this).contacts.email }}'
    
  • Question 95

    What is Non null type assertion operator?

    You can use the non-null type assertion operator to suppress the Object is possibly 'undefined' error. In the following example, the user and contact properties are always set together, implying that contact is always non-null if user is non-null. The error is suppressed in the example by using contact!.email.

    @Component({
    selector: 'my-component',
    template: '<span *ngIf="user"> {{user.name}} contacted through {{contact!.email}} </span>'
    })
    class MyComponent {
    user?: User;
    contact?: Contact;
    
    setData(user: User, contact: Contact) {
    this.user = user;
    this.contact = contact;
    }
    }
    
  • Question 96

    What is type narrowing?

    The expression used in an ngIf directive is used to narrow type unions in the Angular template compiler similar to if expression in typescript. So *ngIf allows the typeScript compiler to infer that the data used in the binding expression will never be undefined.

    @Component({
    selector: 'my-component',
    template: '<span *ngIf="user"> {{user.contact.email}} </span>'
    })
    class MyComponent {
    user?: User;
    }
    
  • Question 97

    How do you describe various dependencies in angular application?

    The dependencies section of package.json with in an angular application can be divided as follow,

    1. Angular packages: Angular core and optional modules; their package names begin @angular/.
    2. Support packages: Third-party libraries that must be present for Angular apps to run.
    3. Polyfill packages: Polyfills plug gaps in a browser's JavaScript implementation.
  • Question 98

    What is zone?

    A Zone is an execution context that persists across async tasks. Angular relies on zone.js to run Angular's change detection processes when native JavaScript operations raise events

  • Question 99

    What is the purpose of common module?

    The commonly-needed services, pipes, and directives provided by @angular/common module. Apart from these HttpClientModule is available under @angular/common/http.

  • Question 100

    What is codelyzer?

    Codelyzer provides set of tslint rules for static code analysis of Angular TypeScript projects. You can run the static code analyzer over web apps, NativeScript, Ionic etc. Angular CLI has support for this and it can be use as below,

    ng new codelyzer
    ng lint
    
  • Question 101

    What is angular animation?

    Angular's animation system is built on CSS functionality in order to animate any property that the browser considers animatable. These properties includes positions, sizes, transforms, colors, borders etc. The Angular modules for animations are @angular/animations and @angular/platform-browser and these dependencies are automatically added to your project when you create a project using Angular CLI.

  • Question 102

    What are the steps to use animation module?

    You need to follow below steps to implement animation in your angular project,

    1. Enabling the animations module: Import BrowserAnimationsModule to add animation capabilities into your Angular root application module(for example, src/app/app.module.ts).
    import { NgModule } from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
    
    @NgModule({
    imports: [
    BrowserModule,
    BrowserAnimationsModule
    ],
    declarations: [ ],
    bootstrap: [ ]
    })
    export class AppModule { }
    
    1. Importing animation functions into component files: Import required animation functions from @angular/animations in component files(for example, src/app/app.component.ts).
    import {
    trigger,
    state,
    style,
    animate,
    transition,
    // ...
    } from '@angular/animations';
    
    1. Adding the animation metadata property: add a metadata property called animations: within the @Component() decorator in component files(for example, src/app/app.component.ts)
    @Component({
    selector: 'app-root',
    templateUrl: 'app.component.html',
    styleUrls: ['app.component.css'],
    animations: [
    // animation triggers go here
    ]
    })
    
  • Question 103

    What is State function?

    Angular's state() function is used to define different states to call at the end of each transition. This function takes two arguments: a unique name like open or closed and a style() function.

    For example, you can write a open state function

    state('open', style({
    height: '300px',
    opacity: 0.5,
    backgroundColor: 'blue'
    })),
    
  • Question 104

    What is Style function?

    The style function is used to define a set of styles to associate with a given state name. You need to use it along with state() function to set CSS style attributes. For example, in the close state, the button has a height of 100 pixels, an opacity of 0.8, and a background color of green.

    state('close', style({
    height: '100px',
    opacity: 0.8,
    backgroundColor: 'green'
    })),
    

    Note: The style attributes must be in camelCase.

  • Question 105

    What is the purpose of animate function?

    Angular Animations are a powerful way to implement sophisticated and compelling animations for your Angular single page web application.

    import { Component, OnInit, Input } from '@angular/core';
    import { trigger, state, style, animate, transition } from '@angular/animations';
    
    @Component({
    selector: 'app-animate',
    templateUrl: `<div [@changeState]="currentState" class="myblock mx-auto"></div>`,
    styleUrls: `.myblock {
    background-color: green;
    width: 300px;
    height: 250px;
    border-radius: 5px;
    margin: 5rem;
    }`,
    animations: [
    trigger('changeState', [
    state('state1', style({
    backgroundColor: 'green',
    transform: 'scale(1)'
    })),
    state('state2', style({
    backgroundColor: 'red',
    transform: 'scale(1.5)'
    })),
    transition('*=>state1', animate('300ms')),
    transition('*=>state2', animate('2000ms'))
    ])
    ]
    })
    export class AnimateComponent implements OnInit {
    
    @Input() currentState;
    
    constructor() { }
    
    ngOnInit() {
    }
    }
    
Get LinkedIn Premium at Rs 399