FrontendDeveloper.in

Angular Interview Questions

  • Question 61

    How do you create directives using CLI?

    You can use CLI command ng generate directive to create the directive class file. It creates the source file(src/app/components/directivename.directive.ts), the respective test file .spec.ts and declare the directive class file in root module.

  • Question 62

    Give an example for attribute directives?

    Let's take simple highlighter behavior as a example directive for DOM element. You can create and apply the attribute directive using below step:

    1. Create HighlightDirective class with the file name src/app/highlight.directive.ts. In this file, we need to import Directive from core library to apply the metadata and ElementRef in the directive's constructor to inject a reference to the host DOM element ,
    import { Directive, ElementRef } from '@angular/core';
    
    @Directive({
    selector: '[appHighlight]'
    })
    export class HighlightDirective {
    constructor(el: ElementRef) {
    el.nativeElement.style.backgroundColor = 'red';
    }
    }
    
    1. Apply the attribute directive as an attribute to the host element(for example, <p>)
    1. Run the application to see the highlight behavior on paragraph element
    ng serve
    
  • Question 63

    What is Angular Router?

    Angular Router is a mechanism in which navigation happens from one view to the next as users perform application tasks. It borrows the concepts or model of browser's application navigation. It enables developers to build Single Page Applications with multiple views and allow navigation between these views.

  • Question 64

    What is the purpose of base href tag?

    The routing application should add <base> element to the index.html as the first child in the <head> tag in order to indicate how to compose navigation URLs. If app folder is the application root then you can set the href value as below

    <base href="/">
    
  • Question 65

    What are the router imports?

    The Angular Router which represents a particular component view for a given URL is not part of Angular Core. It is available in library named @angular/router to import required router components. For example, we import them in app module as below,

    import { RouterModule, Routes } from '@angular/router';
    
  • Question 66

    What is router outlet?

    The RouterOutlet is a directive from the router library and it acts as a placeholder that marks the spot in the template where the router should display the components for that outlet. Router outlet is used like a component,

    <router-outlet></router-outlet>
    <!-- Routed components go here -->
    
  • Question 67

    What are router links?

    The RouterLink is a directive on the anchor tags give the router control over those elements. Since the navigation paths are fixed, you can assign string values to router-link directive as below,

    <h1>Angular Router</h1>
    <nav>
    </nav>
    <router-outlet></router-outlet>
    
  • Question 68

    What are active router links?

    RouterLinkActive is a directive that toggles css classes for active RouterLink bindings based on the current RouterState. i.e, The Router will add CSS classes when this link is active and remove when the link is inactive. For example, you can add them to RouterLinks as below.

    <h1>Angular Router</h1>
    <nav>
    </nav>
    <router-outlet></router-outlet>
    
  • Question 69

    What is router state?

    RouterState is a tree of activated routes. Every node in this tree knows about the "consumed" URL segments, the extracted parameters, and the resolved data. You can access the current RouterState from anywhere in the application using the Router service and the routerState property.

    @Component({templateUrl:'template.html'})
    class MyComponent {
    constructor(router: Router) {
    const state: RouterState = router.routerState;
    const root: ActivatedRoute = state.root;
    const child = root.firstChild;
    const id: Observable<string> = child.params.map(p => p.id);
    //...
    }
    }
    
  • Question 70

    What are router events?

    During each navigation, the Router emits navigation events through the Router.events property allowing you to track the lifecycle of the route.

    The sequence of router events is as below,

    1. NavigationStart,
    2. RouteConfigLoadStart,
    3. RouteConfigLoadEnd,
    4. RoutesRecognized,
    5. GuardsCheckStart,
    6. ChildActivationStart,
    7. ActivationStart,
    8. GuardsCheckEnd,
    9. ResolveStart,
    10. ResolveEnd,
    11. ActivationEnd
    12. ChildActivationEnd
    13. NavigationEnd,
    14. NavigationCancel,
    15. NavigationError
    16. Scroll
  • Question 71

    What is activated route?

    ActivatedRoute contains the information about a route associated with a component loaded in an outlet. It can also be used to traverse the router state tree. The ActivatedRoute will be injected as a router service to access the information. In the below example, you can access route path and parameters,

    @Component({...})
    class MyComponent {
    constructor(route: ActivatedRoute) {
    const id: Observable<string> = route.params.pipe(map(p => p.id));
    const url: Observable<string> = route.url.pipe(map(segments => segments.join('')));
    // route.data includes both `data` and `resolve`
    const user = route.data.pipe(map(d => d.user));
    }
    }
    
  • Question 72

    How do you define routes?

    A router must be configured with a list of route definitions. You configures the router with routes via the RouterModule.forRoot() method, and adds the result to the AppModule's imports array.

     const appRoutes: Routes = [
    { path: 'todo/:id',      component: TodoDetailComponent },
    {
    path: 'todos',
    component: TodosListComponent,
    data: { title: 'Todos List' }
    },
    { path: '',
    redirectTo: '/todos',
    pathMatch: 'full'
    },
    { path: '**', component: PageNotFoundComponent }
    ];
    
    @NgModule({
    imports: [
    RouterModule.forRoot(
    appRoutes,
    { enableTracing: true } // <-- debugging purposes only
    )
    // other imports here
    ],
    ...
    })
    export class AppModule { }
    
  • Question 73

    What is the purpose of Wildcard route?

    If the URL doesn't match any predefined routes then it causes the router to throw an error and crash the app. In this case, you can use wildcard route. A wildcard route has a path consisting of two asterisks to match every URL.

    For example, you can define PageNotFoundComponent for wildcard route as below

    { path: '**', component: PageNotFoundComponent }
    
  • Question 74

    Do I need a Routing Module always?

    No, the Routing Module is a design choice. You can skip routing Module (for example, AppRoutingModule) when the configuration is simple and merge the routing configuration directly into the companion module (for example, AppModule). But it is recommended when the configuration is complex and includes specialized guard and resolver services.

  • Question 75

    What is Angular Universal?

    Angular Universal is a server-side rendering module for Angular applications in various scenarios. This is a community driven project and available under @angular/platform-server package. Recently Angular Universal is integrated with Angular CLI.

Get LinkedIn Premium at Rs 399