Skip to main content

Angular start 2

 Module in Angular:

Modules are logical boundaries in your application and the application is divided into separate modules to separate the functionality of your application. Let's take an example of app.module.ts root module declared with @NgModule decorator as below,


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

import { BrowserModule } from '@angular/platform-browser';

import { AppComponent }  from './app.component';


@NgModule ({

   imports:      [ BrowserModule ],

   declarations: [ AppComponent ],

   bootstrap:    [ AppComponent ],

   providers: []

})

export class AppModule { }

The NgModule decorator has five important(among all) options


The imports option is used to import other dependent modules. The BrowserModule is required by default for any web-based angular application

The declarations option is used to define components in the respective module

The bootstrap option tells Angular which Component to bootstrap in the application

The providers option is used to configure set of injectable objects that are available in the injector of this module.

The entryComponents option is a set of components dynamically loaded into the view.



What are decorators in angular?


A class decorator is a decorator that appears immediately before a class definition, which declares the class to be of the given type and provides metadata suitable to the type


A Decorator is a special kind of declaration that can be attached to a class declaration, method, accessor, property, or parameter. Decorators use the form @expression, where the expression must evaluate to a function that will be called at runtime with information about the decorated declaration.

The following list of decorators comes under class decorators,


@Component()


@Directive()


@Pipe()


@Injectable()


@NgModule()


What are selectores in Angular?

If we say in simple term selector is name which is used in our view like html tag. as we all know angular2 is component based. so selector is just provide the name of the component which is being called by its className in the directives list and called by selector name in the view of the another component like this:-


Selectors are like CSS selectors. They can be attribute selectors, tag selectors, class selectors, id selectors and combinations of these. 

If we say in simple term selector is name which is used in our view like html tag. as we all know angular2 is component based. so selector is just provide the name of the component which is being called by its className in the directives list and called by selector name in the view of the another component like this:-

Selectors are like CSS selectors. They can be attribute selectors, tag selectors, class selectors, id selectors and combinations of these. 
name: Use element name as selector. 'app-root' use as <app-root></app-root>

.class: Use a class name as a selector.'.app-root' use as <div class="app-root">

[attribute]: Use the name of the attribute as a selector.
'[app-root]' use as   <div app-root></div>


What happens in aot?

Ahead-of-Time (AOT) is a type of compilation that compiles your app at build time. For AOT compilation, include the --aot option with the ng build or ng serve command as below,

ng build --aot
ng serve --aot
Note: The ng build command with the --prod meta-flag (ng build --prod) compiles with AOT by default.

Below are the list of AOT benefits,

  1. Faster rendering: The browser downloads a pre-compiled version of the application. So it can render the application immediately without compiling the app.
  2. Fewer asynchronous requests: It inlines external HTML templates and CSS style sheets within the application javascript which eliminates separate ajax requests.
  3. Smaller Angular framework download size: Browser doesn't require downloading the Angular compiler. Hence it dramatically reduces the application payload.
  4. Detect template errors earlier: Detects and reports template binding errors during the build step itself
  5. Better security: It compiles HTML templates and components into JavaScript. So there won't be any injection attacks


Commonds:
1)Generate new component:
ng g c componentname->componentname-component.ts

2)Generate new directive
ng g d directivename->directivename-directive.ts

3)Generate new service
ng g s common->common-service.ts

4)Generate new module
ng g m shared->shared-module.ts

5)Generate new pipe
ng g p trans-> trans-pipe.ts


Comments

Popular posts from this blog

Node.js: Extract text from image using Tesseract.

In this article, we will see how to extract text from images using Tesseract . So let's start with this use-case, Suppose you have 300 screenshot images in your mobile which has an email attribute that you need for some reason like growing your network or for email marketing. To get an email from all these images manually into CSV or excel will take a lot of time. So now we will check how to automate this thing. First, you need to install Tesseract OCR( An optical character recognition engine ) pre-built binary package for a particular OS. I have tested it for Windows 10. For Windows 10, you can install  it from here. For other OS you make check  this link. So once you install Tesseract from windows setup, you also need to set path variable probably, 'C:\Program Files\Tesseract-OCR' to access it from any location. Then you need to install textract library from npm. To read the path of these 300 images we can select all images and can rename it to som...

Globant part 1

 1)call,apply,bind example? Ans: a. call Method: The call method is used to call a function with a given this value and arguments provided individually. Javascript code: function greet(name) {   console.log(`Hello, ${name}! I am ${this.role}.`); } const person = {   role: 'developer' }; greet.call(person, 'Alice'); // Output: Hello, Alice! I am developer. In this example, call invokes the greet function with person as the this value and passes 'Alice' as an argument. b. apply Method: The apply method is similar to call, but it accepts arguments as an array. Javascript code: function introduce(language1, language2) {   console.log(`I can code in ${language1} and ${language2}. I am ${this.name}.`); } const coder = {   name: 'Bob' }; introduce.apply(coder, ['JavaScript', 'Python']); // Output: I can code in JavaScript and Python. I am Bob. Here, apply is used to invoke introduce with coder as this and an array ['JavaScript', 'Pyt...

Maximizing MongoDB Performance: A Guide to Effective Indexing Strategies

Introduction: MongoDB, a leading NoSQL database, offers unparalleled flexibility and scalability. However, achieving optimal query performance in MongoDB often relies on implementing effective indexing strategies. In this article, we'll delve into various indexing techniques and provide real-world examples to demonstrate their impact on query performance. Single Field Index: Single field indexes are ideal for accelerating queries that filter, sort, or search based on a specific field. Let's consider a scenario where we have a collection of user profiles, and we frequently query users by their username field: db.users.createIndex({ "username": 1 }) This index significantly speeds up queries like: db.users.find({ "username": "john_doe" }) Compound Index: Compound indexes are invaluable when queries involve multiple fields. Suppose we have a collection of products and often filter by both category and price: db.products.createIndex({ "category...