Skip to main content

Angular Interview Question and Answer

 

Angular Interview Questions for Freshers

1. Why were client-side frameworks like Angular introduced?

Back in the day, web developers used VanillaJS and jQuery to develop dynamic websites but, as the logic of one's website grew, the code became more and more tedious to maintain. For applications that use complex logic, developers had to put in extra effort to maintain the separation of concerns for the app. Also, jQuery did not provide facilities for data handling across views.

For tackling the above problems, which made life easier for the developers by handling the separation of concerns and dividing code into smaller bits of information (In the case of Angular, called Components).

Client-side frameworks allow one to develop advanced web applications like Single-Page-Application. Not that we cannot develop SPAs using VanillaJS, but by doing so, the development process becomes slower.

2. How does an Angular application work?

Every Angular app consists of a file named angular.json. This file will contain all the configurations of the app. While building the app, the builder looks at this file to find the entry point of the application. Following is an image of the angular.json file:

  "build": {
        "builder": "@angular-devkit/build-angular:browser",
        "options": {
          "outputPath": "dist/angular-starter",
          "index": "src/index.html",
          "main": "src/main.ts",
          "polyfills": "src/polyfills.ts",
          "tsConfig": "tsconfig.app.json",
          "aot": false,
          "assets": [
            "src/favicon.ico",
            "src/assets"
          ],
          "styles": [
            "./node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css",
            "src/style.css"
          ]
        }
      }

 Inside the build section, the main property of the options object defines the entry point of the application which in this case is main.ts.

The main.ts file creates a browser environment for the application to run, and, along with this, it also calls a function called bootstrapModule, which bootstraps the application. These two steps are performed in the following order inside the main.ts file: 

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
platformBrowserDynamic().bootstrapModule(AppModule)

In the above line of code, AppModule is getting bootstrapped.

The AppModule is declared in the app.module.ts file. This module contains declarations of all the components.

Below is an example of app.module.ts file:    

  import { BrowserModule } from '@angular/platform-browser';
      import { NgModule } from '@angular/core';
      import { AppComponent } from './app.component';

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

 As one can see in the above file, AppComponent is getting bootstrapped.

This component is defined in app.component.ts file. This file interacts with the webpage and serves data to it.

Below is an example of app.component.ts fiale:

 import { Component } from '@angular/core';

      @Component({
        selector: 'app-root',
        templateUrl: './app.component.html',
        styleUrls: ['./app.component.css']
      })
      export class AppComponent {
        title = 'angular';
      }   

 Each component is declared with three properties:

  • Selector - used for accessing the component
  • Template/TemplateURL - contains HTML of the component
  • StylesURL - contains component-specific stylesheets

After this, Angular calls the index.html file. This file consequently calls the root component that is app-root. The root component is defined in app.component.ts. This is how the index.html file looks:

<!doctype html>
      <html lang="en">
      <head>
        <meta charset="utf-8">
        <title>Angular</title>
        <base href="/">
        <meta name="viewport" content="width=device-width, initial-scale=1">
      </head>
      <body>
        <app-root></app-root>
      </body>
      </html>

What are the advantages of Angular over React?

Angular vs React: Check out the differences

AngularReact
Angular supports bidirectional data binding as well as mutable data.React only supports unidirectional and immutable data binding.
The biggest benefit of Angular is that it enables dependency injection.React allows us to either accomplish it ourselves or with the aid of a third-party library.
Angular can be used in both mobile and web development.React can only be used in UI development only.
Angular features a wide wide range of tools, libraries, frameworks, plugins, and so on that make development faster and more fun.In React we can use third-party libraries for any features.
Angular uses Typescript.React uses Javascript.

5. List out differences between AngularJS and Angular?

Check out the differences between AngularJS and Angular below. For More Information, Click here.

FeaturesAngularJSAngular
ArchitectureAngularJS uses MVC or Model-View-Controller architecture, where the Model contains the business logic, the Controller processes information and the View shows the information present in the Model.Angular replaces controllers with Components. Components are nothing but directives with a predefined template.
LanguageAngularJS uses JavaScript language, which is a dynamically typed language.Angular uses TypeScript language, which is a statically typed language and is a superset of JavaScript. By using statically typed language, Angular provides better performance while developing larger applications.
Mobile SupportAngularJS does not provide mobile support.Angular is supported by all popular mobile browsers.
StructureWhile developing larger applications, the process of maintaining code becomes tedious in the case of AngularJS.In the case of Angular, it is easier to maintain code for larger applications as it provides a better structure.
Expression SyntaxWhile developing an AngularJS application, a developer needs to remember the correct ng-directive for binding an event or a property. Whereas in Angular, property binding is done using "[ ]" attribute and event binding is done using "( )" attribute.

Comments

Popular posts from this blog

ngModel in Angular

 ngModel :-                                                             ================== ngModel is a directive in angular that allow two way data binding. it bind th value of an HTML control (such as input, select, or textarea) to a prperty on the component and updates that property when the user changes th evalue in the control. To use "ngModel" in Angular, you need to import the formModule in your module And add it to the "imports" array. Then you can add the "ngModel" directive to an HTML controlin your template and  bind it to a property on ypur component using square brackets. Here is an example of using "ngModel" in a simple input form: Example:- <!-- component.html --> <input [(ngModel)]="name"> <!-- component.ts --> export class MyComponent {   name: string; } ================================== E...