Angular is a huge framework, no other platform nowadays can compare with angular as it has big support of libraries and resources in the market. It is a big part of web development today.
Angular is complete solution to any kind of web applications requirements like server side rendering , progressive web app etc. with component based platform like angular we can create any kind of single page application that creates more productivity.
Today i am going to talk about components and module in angular application what we can do with them or what they both have things for us to build an awesome , well structured angular application.
These both concepts are back bone of Angular because Angular is a component based framework and we can give look and feel using these components. Modules have some great features to configure everything going within it.
Components
ng g c example
- ng- angular
- g-generate
- c-component
- example- you want to name a component
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.css']
})
export class ExampleComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
}
A component where we can create our own functions it is also known as component
decorator @component where this decorator provides some additional
metadata which determines how a component can be processed, how it is used at
run time and how it should be instantiated.
Features of a component
- Selector: where an angular application associate the component with an element that can directly be used in DOM HTML
- templateUrl or template - templateUrl refers the HTML file which is bind to a component and that can be responsible for user interface of a component. we can specify HTML directly into a component by using template attribute.
- styleUrls or style: this refers a style sheet which is responsible for styles for the component template. Style can also be internal by using style attribute and CSS format directly in the ''.
- ChangeDetection: Change-detection strategy is used to check detections and let angular app know about the those detections , here you may find explained change detection strategy concept.
- Animations: this is used to provide various animations in between components.
- Encapsulations: encapsulation policy for templates and CSS, these can be one of ViewEncapsulation.Emulated, ViewEncapsulation.None and ViewEncapsulation.ShadowDom.it is used to defines if template and css can affect the whole application or not and we can handle using three encapsulations strategy.
- Interpolation: this is used for property binding or make any function working in templates , we may use interpolation to define any expression etc.
- Providers : this is used to create service instance and we can injection them into classes.
Module
- declarations: Here we define components, directives, and pipes that belong to this NgModule. (we only define a component in one module)
- exports: The subset of declarations that can be visible and usable in the component templates of other NgModules.
- imports: define all modules that are needed to run the component's template of that module and other modules whose exported classes are needed by component templates declared in this NgModule.
- providers: we uses services and all other providers to specify in providers array at global level of module (we can define providers at component level that is preferred).
- bootstrap: This is responsible to render the main view of the application by calling the root component which hosts all other app views. Only root NgModule can be bootstrapped to main.ts or should set this property.
Types of modules
- Features Module
- Routing Module
- Service Module
- Widget Module
- Shared Module
1 Comments
I really enjoyed reading your blog. It was very well written and easy to understand. Unlike other blogs that I have read which are actually not very good. Thank you so much!
ReplyDeleteHire Dedicated Angular Developer