Angular JS is not component based but Angular 2 is component based.
Angular JS app will have View first and for every view we have assigned Controller which is having business logic, it means View is the one who decide the Controller and Model Logic.
Angular 2 app will have Root Component first and for every component we will have template i.e. View and will get Model logic for the template.
Components are the building blocks of an Angular 2 Application.
3.TypeScript
TypeScript is a Super Set of JavaScript.
TypeScript is an open source Language.
TypeScript is maintained by Microsoft.
TypeScript runs on any JavaScript engine.
As we know, we are not able to compile the JavaScript so its so hard to find out the JavaScript error at run time instead if we can write the code in typescript and compiles to JavaScript and in case of any error will come to know while compilation.
TypeScript used static typing to help for type checking at compile time.
For run time, Transcompiler will transpiled TypeScript code to JavaScript.
TypeScript can be used to develop for Server side as well as Client side execution.
TypeScript code file has an extension “.ts”. e.g. pavan.ts
TypeScript code complied into JavaScript code using Transcompiler
After installing type script compiler, we need to run the command for file pavan.ts
Command: tsc pavan.ts
The above command will create a javascript file i.e. pavan.js and can be included in HTML.
It is used as package manager for any project and installer.
It is used to manage dependencies.
It can be used to install all the dependencies of a Project.
5.Installing NPM and TypeScript
NPM is installed with Node JS.
We need to stall NPM in the same folder where we have Angular2 Project.
After installing Node JS, please go to command prompt and hit… npm -v
C:/>node -v or C:/>npm -v
To install the TypeScript after npm use following command.
C:/>npm install -g typescript
6.How to run the type script File
Install NPM using node js
Please check and confirm the installation using following command
C:\>node -v
C:\>npm -v
Install TypeScript using following command
C:\>npm install -g typescript
Check the typescript version
C:\>tsc -version
Create type script file “second.tsc” in C drive -> Angular2 Folder with below Code.
var languageName:string="Angular2";
console.log(languageName);
Go to command prompt and fire the below command
C:\Angular2>tsc second.ts
Please check the Angular2 folder, you should see the second.js file, which is compile file of second.ts typescript file.
To run this javascript file you can use following command
C:\Angular2>node second.js
7.Simple Angular 2 Program
Install NPM using node js
Please check and confirm the installation using following command
C:\>node -v
C:\>npm -v
Install TypeScript using following command
C:\>npm install -g typescript
Check the typescript version
C:\>tsc -version
Install Angular -CLI
C:\>npm install -g @angular/cli
Angular CLI is used to create Projects, library code and perform development task i.e. testing and deployment.
It will download the structure of Angular 2 like following folders
e2e
src
node_modules (All the node packages coming from NPM i.e npm install command)
if we use go to any folder and fire the command i.e. npm install, it will install the npm modules in that folder with new folder name node_modules provided that there should be package.json and angular.json files.
package.json(Files)
angular.json (Files)
To create new Angular Project using Angular-CLI
ng new MyProject
It will create a whole Angular 2 project structure.
How to run the Angular App i.e. my-app
cd my-app
ng serve - - open
ng serve command launches the server and build the app.
- - open will open the browser and hit http://localhost:4200
8.Angular2 Project Folders and Files
If we use CLI for Angular2 Project code, we will get following main folders and files
Files
main.ts File
angular.js File
main.ts file loaded by angular.js
main.ts contains the reference of Root App Module name i.e. AppModule (app.module.ts File)
Root App Module contains the reference of App Component(app.component.ts File)
Folder
src Folder- contains (app, asset)
app - Most of the Code will be here
asset – We can use this folder as a asset for images, css etc.
node_modules Folder – contains all the npm packages and libraries
e2e – end to end testing
9.Process Flow for the Angular Application
Angular.js - Contains configuration information for build system which will provide the files to build system when we fire the command ng build or ng serve.
Package.json – Contains information about all the packages which are going to used angular application.
All the NPM packages contains a file, usually in the project root, called package.json file.
Package-lock.json – It is automatically generated for any operation where npm modifies either node module tree or package.json
Process:
First Angular.json file load the main.ts file
The main.ts file loads app modules.
Through app module, angular will get app component.
And then index.html will load app component which has template.
10.Focused Items of Angular 2
Components
Modules
Meta Data
Templates
Data Binding
Dependency Injection
Services
Directives
11.Angular Component
TypeScript Class
We need to create a class and need to export it.
Export class AppComponent {
userName: string =” Pavan” ;
constructor () {
}
}
We can have all the business logic in this class.
Decorator
Decorator describes the metadata, which defines properties of component using @Component Decorator.
@Component decorator is of TypeScript.
@Component decorator decorates a TypeScript class as a component.
IT is a function having object as a parameter.
@Component({
Selector :’mainapp-container’,
Template:’<h5> {{ userName }}</h5>’
})
Decorator contains following properties
Template View
TemplateURL
Directives
StyleUrls
Pipes
Selector
Providers
Import required Libraries and modules to create the Component