Features of Angular 2
Components: focus is shifted from controllers in earlier version to components. components help to build applications into many modules and make application easy to maintain.
TypeScript: newer version of angular is based on typescript it is superset of javascript and is maintained by Microsoft.
Services: it is piece of code that can be shared by different components of an application. for example if there is data component that picked up data from DB, then it can be set as shared service to use it across application.
Angular 2 Directives:
directive is custom HTML element that is used to extend power of HTML. Angular 2 has below directives as part of BrowserModule module
ngIf
syntax: *ngIf = 'expression'
ngFor
syntax: *ngFor = 'let variable of variableList'
These are used as part of html elements to display based on condition specified in for loop or if condition.
routing in angular
based on option user choose on main page the corresponding component gets rendered.
what is CLI
Command line interface used to create angular application using commands. it also helps to create unit tests or end to end tests.
Dependency Injection
Ability to add functionality of component at runtime.
example:
create a class with injectable decorator and in any module where you want to use the service, you need to define as provider in the component decorator.
Example:
@Injectable() export class classX { } @Component ({ providers : [classX] })
package.json file contain information about Angular 2 project.systemjs.config.json file loads all necessary infomation without need to add script tag to html pages.app.module.ts
import {NgModule} from '@angular/core'; import {BrowserModule} from '@angular/platform-browser'; import {AppComponent} from './app.component'; @NgModule({ imports : [BrowserModule], declarations : [AppComponent], bootstrap: [AppComponent] }) export class AppModule { }
The import statement is used to import functionality from the existing modules. Thus, the first 3 statements are used to import the NgModule, BrowserModule and AppComponent modules into this module.The NgModule decorator is used to later on define the imports, declarations, and bootstrapping options.The BrowserModule is required by default for any web based angular application.The bootstrap option tells Angular which Component to bootstrap in the application.Convert String to lowercase/uppercase/Date/currency/percentage
The name of this tutorial is {{tutorialname}} The first topic is {{tutorialname | lowercase}} converting to uppercase {{tutorialname | uppercase}} date value is {{newdate | date:"MM/dd/yy"}} amount {{amount | percent}} final amount {{amount | currency}}
ngOnChanges event gets called when a data bound property changes its value.ngOnInit event : during initialization of component.ngDoCheck eventngAfterContentInit event gets called when external content gets loaded to a component view.npm = node package manager is used to download packages/dependencies on other components useful to run angular project.How to setup Angular 2 Environment:Go to below website, download and install node.jsrun below command from command prompt to check version of node$node versiondownload Visual Studio Code tool / editor from below linkSample Application Run:create a directory with name Project_practice directory and Demo folder inside it.now go to command prompt and traverse to Project_practice directory and run below command to clone the git project$git clone https:
//github.com/angular/quickstart Demo
Now go to Demo directory and run npm install to install necessary packages to run AngularJS application.
Now run below command to start the web applicationapp.module.ts file in src/app directory is root module class. it contains NgModule decorator to set imports declarations, bootstrap.Each Angular 2 application made of modules and the root module is made of components. each component specific to each task.Components are a logical piece of code for Angular JS application. A Component consists of the following:Template − This is used to render the view for the application. This contains the HTML that needs to be rendered in the application.Class − This is like a class defined in any language such as C++/Java in typescript. This contains properties and methods. This has the code which is used to support the view.Metadata − This has the extra data defined for the Angular class. It is defined with a decorator.In the component we can even use templateUrl instead of template by passing html page url as below: templateUrl: 'app/app.component.html'here selector is the custom html tag to be used in index.html as below. template is the view. class got exported so that we can use it in other components.Data Bindingexample to display images and binding themapp.component.tsapp.component.htmlForms:Angular CLI (https://cli.angular.io/): The Angular CLI makes it easy to create an application that already works, right out of the box. It already follows our best practices! install python and run below command from terminal to install angular cli npm install -g @angular/cli this make creating project easy: ng new Demo3 //It will automatically create the files and start downloading the necessary npm packages. Demo3 is project name here. ng serve // this will run the projectto generate distng build // to generate dist directory asset.config filenode server.js // to read server.js and run output in 8080 portDependency Injection:Demo3 project implemented by madhu from https://www.tutorialspoint.com/angular2/angular2_dependency_injection.htmData Binding on html tag example:which is binding on the fly is the use of the input html tag. It just displays the data as the data is being typed in the html tag. Make the following changes to the app/app.component.html file which is your template file.more details of above code:[value] = ”username” − This is used to bind the expression username to the input element’s value property.(input) = ”expression” − This a declarative way of binding an expression to the input element’s input event.username = $event.target.value − The expression that gets executed when the input event is fired.(click) = "method call" this will call the method defined in corresponding component class for which this template belongs to.Data Transformations that can be used in displaying values in template/html:Custom Pipes in Angular:Angular 2 also has the facility to create custom pipes. The general way to define a custom pipe is as follows.Ensure the custom pipe is added to declarations of app.module.ts, so that we can use it in any of the component template html file.The Root Module (app.module.ts)How Angular2 flow works(first it executes main.ts which invokes app.module which loads component specified in bootstrap then loads the component code)main.ts --→ app.module.ts --→ app.component.tscommand to run the application:ng servecommand to run karma test cases:ng testSome commands in angular:how to check the version of Angular CLIEx: ng --versionTo remove angular/cliEx: npm remove @angular/cliTo install angular/cli:npm install --save-dev@angular/cli@latestTo check version of webpack:npm list webpackto remove webpack:Ex: npm remove webpackTo install webpack of specific version only:npm install webpack@3.11.0install with --save-devnpm install --save-dev @angular/cli@v6.0.0-rc.4References:
No comments:
Post a Comment