Angular 4 - Western Michigan University

2y ago
27 Views
5 Downloads
4.01 MB
137 Pages
Last View : 26d ago
Last Download : 3m ago
Upload by : Joao Adcock
Transcription

Angular 4i

Angular 4About the TutorialAngular 4 is a JavaScript framework for building web applications and apps in JavaScript,html, and TypeScript, which is a superset of JavaScript. Angular provides built-in featuresfor animation, http service, and materials which in turn has features such as autocomplete, navigation, toolbar, menus, etc. The code is written in TypeScript, whichcompiles to JavaScript and displays the same in the browser.AudienceThis tutorial is designed for software programmers who want to learn the basics of Angular4 and its programming concepts in a simple and easy manner. This tutorial will give youenough understanding on the various functionalities of Angular 4 with suitable examples.PrerequisitesBefore proceeding with this tutorial, you should have a basic understanding of HTML, CSS,JavaScript, TypeScript, and Document Object Model (DOM).Copyright & Disclaimer Copyright 2017 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of Tutorials Point (I)Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republishany contents or a part of contents of this e-book in any manner without written consentof the publisher.We strive to update the contents of our website and tutorials as timely and as precisely aspossible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of ourwebsite or its contents including this tutorial. If you discover any errors on our website orin this tutorial, please notify us at contact@tutorialspoint.comi

Angular 4Table of ContentsAbout the Tutorial .iAudience.iPrerequisites .iCopyright & Disclaimer .iTable of Contents . ii1.Angular 4 – Overview .1Why Angular4 and Not Angular3? .12.Angular 4 – Environment Setup .33.Angular 4 – Project Setup .64.Angular 4 – Components . 265.Angular 4 – Module . 356.Angular 4 – Data Binding . 387.Angular 4 – Event Binding . 458.Angular 4 – Templates . 519.Angular 4 – Dire ctives . 55How to Create Custom Directives?. 5510. Angular 4 – Pipes . 59How to Create a Custom Pipe? . 6411. Angular 4 – Routing . 6712. Angular 4 – Services. 7213. Angular 4 – Http Service . 8114. Angular 4 – Forms . 88Template Driven Form . 88Model Driven Form. 91Form Validation. 9415. Angular 4 – Anim ations. 99ii

Angular 416. Angular 4 – Materials . 10517. Angular 4 – CLI . 11418. Angular 4 – Examples . 115iii

1. Angular 4 – OverviewAngular 4There are three major releases of Angular. The first version that was released is Angular1,which is also called AngularJS. Angular1 was followed by Angular2, which came in with alot of changes when compared to Angular1.The structure of Angular is based on the components/services architecture. AngularJS wasbased on the model view controller. Angular 4 released in March 2017 proves to be amajor breakthrough and is the latest release from the Angular team after Angular2.Angular 4 is almost the same as Angular 2. It has a backward compatibility with Angular2. Projects developed in Angular 2 will work without any issues with Angular 4.Let us now see the new features and the changes made in Angular 4.Why Angular4 and Not Angular3?The Angular team faced some versioning issues internally with their modules and due tothe conflict they had to move on and release the next version of Angular – the Angular4.Let us now see the new features added to Angular 4:ngIfAngular2 supported only the if condition. However, Angular 4 supports the if elsecondition as well. Let us see how it works using the ng-template. span *ngIf "isavailable; else condition1" Condition is valid. /span ng-template #condition1 Condition is invalid /ng-template as keyword in for loopWith the help of as keyword you can store the value as shown below: div *ngFor "let i of months slice:0:5 as total" Months: {{i}} Total: {{total.length}} /div The variable total stores the output of the slice using the as keyword.Animation PackageAnimation in Angular 4 is available as a separate package and needs to be imported from@angular/animations. In Angular2, it was available with @angular/core. It is still keptthe same for its backward compatibility aspect.1

Angular 4TemplateAngular 4 uses ng-template as the tag instead of template ; the latter was usedin Angular2. The reason Angular 4 changed template to ng-template is becauseof the name conflict of the template tag with the html template standard tag. Itwill deprecate completely going ahead. This is one of the major changes in Angular 4.TypeScript 2.2Angular 4 is updated to a recent version of TypeScript, which is 2.2. This helps improvethe speed and gives better type checking in the project.Pipe Title CaseAngular 4 has added a new pipe title case, which changes the first letter of each word intouppercase. div h2 {{ 'Angular 4 titlecase' titlecase }} /h2 /div The above line of code generates the following output – Angular 4 Titlecase.Http Search ParametersSearch parameters to the http get api is simplified. We doURLSearchParams for the same as was being done in Angular2.notneed to callSmaller and Faster AppsAngular 4 applications are smaller and faster when compared to Angular2. It uses theTypeScript version 2.2, the latest version which makes the final compilation small in size.2

2. Angular 4 – Environment SetupAngular 4In this chapter, we will discuss the Environment Setup required for Angular 4. To installAngular 4, we require the following: Nodejs Npm Angular CLI IDE for writing your codeNodejs has to be greater than 4 and npm has to be greater than 3.NodejsTo check if nodejs is installed on your system, type node –v in the terminal. This will helpyou see the version of nodejs currently installed on your system.C:\ node –vv6.11.0If it does not print anything, install nodejs on your system. To install nodejs, go thehomepage https://nodejs.org/en/download/ of nodejs and install the package based onyour OS.The homepage of nodejs will look like the following:3

Angular 4Based on your OS, install the required package. Once nodejs is installed, npm will also getinstalled along with it. To check if npm is installed or not, type npm –v in the terminal. Itshould display the version of the npm.C:\ npm –v5.3.0Angular 4 installations are very simple with the help of angular CLI. Visit the homepagehttps://cli.angular.io/ of angular to get the reference of the command.Type npm install –g @angular/cli, to install angular cli on your system.4

Angular 4You will get the above installation in your terminal, once Angular CLI is installed. You canuse any IDE of your choice, i.e., WebStorm, Atom, Visual Studio Code, etc.The details of the project setup is explained in the next chapter.5

3. Angular 4 – Project SetupAngular 4AngularJS is based on the model view controller, whereas Angular 2 is based on thecomponents structure. Angular 4 works on the same structure as Angular2 but is fasterwhen compared to Angular2.Angular4 uses TypeScript 2.1 version whereas Angular 2 uses TypeScript version 1.8. Thisbrings a lot of difference in the performance.To install Angular 4, the Angular team came up with Angular CLI which eases theinstallation. You need to run through a few commands to install Angular 4.Go to this site https://cli.angular.io to install Angular CLI.To get started with the installation, we first need to make sure we have nodejs and npminstalled with the latest version. The npm package gets installed along with nodejs.Go to the nodejs site https://nodejs.org/en/.6

Angular 4The latest version of Nodejs v6.11.0 is recommended for users. Users who already havenodejs greater than 4 can skip the above process. Once nodejs is installed, you can checkthe version of node in the command line using the command, node –v, as shown below:The command prompt shows v6.11.0. Once nodejs is installed, npm will also get installedalong with it.7

Angular 4To check the version of npm, type command npm –v in the terminal. It will display theversion of npm as shown below.The version of npm is 3.10.10. Now that we have nodejs and npm installed, let us run theangular cli commands to install Angular 4. You will see the following commands on thewebpage:npm install -g @angular/cling newAngular 4-app//command to install angular 4// name of the projectcd my-dream-appng serve8

Angular 4Let us start with the first command in the command line and see how it works.To start with, we will create an empty directory wherein, we will run the Angular CLIcommand.Enter the above command to install Angular 4. The installation process will start and willtake a few minutes to complete.9

Angular 4Once the above command to install is complete, the following Command Prompt appears:We have created an empty folder ProjectA4 and installed the Angular CLI command. Wehave also used -g to install Angular CLI globally. Now, you can create your Angular 4project in any directory or folder and you don’t have to install Angular CLI project wise, asit is installed on your system globally and you can make use of it from any directory.Let us now check whether Angular CLI is installed or not. To check the installation, run thefollowing command in the terminal:ng -v10

Angular 4We get the @angular/cli version, which is at present 1.2.0. The node version running is6.11.0 and also the OS details. The above details tell us that we have installed angular clisuccessfully and now we are ready to commence with our project.We have now installed Angular 4. Let us now create our first project in Angular 4. To createa project in Angular 4, we will use the following command:ngnewprojectnameWe will name the project ng new Angular 4-app.Let us now run the above command in the command line.11

Angular 4The project Angular 4-app is created successfully. It installs all the required packagesnecessary for our project to run in Angular 4. Let us now switch to the project created,which is in the directory Angular 4-app. Change the directory in the command line - cdAngular 4-app.We will use Visual Studio Code IDE for working with Angular 4; you can use any IDE, i.e.,Atom, WebStorm, etc.To download Visual Studio Code, go to https://code.visualstudio.com/ and click Downloa dfor Windows.12

Angular 4Click Download for Windows for installing the IDE and run the setup to start using IDE.The Editor looks as follows:We have not started any project in it . Let us now take the project we have created usingangular-cli.13

Angular 4We will consider the Angular 4-app project. Let us open the Angular 4-app and see howthe folder structure looks like.14

Angular 4Now that we have the file structure for our project, let us compile our project with thefollowing command:ngserve15

Angular 4The ng serve command builds the application and starts the web server.The web server starts on port 4200. Type the url http://localhost:4200/ in the browserand see the output. Once the project is compiled, you will receive the following output:16

Angular 417

Angular 4Once you run http://localhost:4200/ in the browser, you will be directed to the followingscreen:Let us now make some changes to display the following content:“Welcome to Angular 4 project”We have made changes in the files – app.component.html and app.component.ts. Wewill discuss more about this in our subsequent chapters.Let us complete the project setup. If you see we have used port 4200, which is the defaultport that angular–cli makes use of while compiling. You can change the port if you wishusing the following command:ngserve--host 0.0.0.0–port420518

Angular 4The Angular 4 app folder has the following folder structure: e2e - end to end test folder. Mainly e2e is used for integration testing and helpsensure the application works fine. node modules - The npm package installed is node modules. You can open thefolder and see the packages available. src – This folder is where we will work on the project using Angular 4.The Angular 4 app folder has the following file structure: .angular-cli.json - It basically holds the project name, version of cli, etc. .editorconfig - This is the config file for the editor.gitignore - A .gitignore file should be committed into the repository, in order toshare the ignore rules with any other users that clone the repository. karma.conf.js - This is used for unit testing via the protractor. All the informat ionrequired for the project is provided in karma.conf.js file. package.json - The package.json file tells which libraries will be installed intonode modules when you run npm install.At present, if you open the file in the editor, you will get the following modules added init."@angular/animations": " 4.0.0","@angular/common": " 4.0.0","@angular/compiler": " 4.0.0","@angular/core": " 4.0.0","@angular/forms": " 4.0.0","@angular/http": " 4.0.0","@angular/platform-browser": " 4.0.0","@angular/platform-browser-dynamic": " 4.0.0","@angular/router": " 4.0.0",In case you need to add more libraries, you can add those over here and run the npminstall command. protractor.conf.js - This is the testing configuration required for the application. tsconfig.jsoncompilation. tslint.json - This is the config file with rules to be considered while compiling.- This basically contains the compiler options required during19

Angular 4The src folder is the main folder, which internally has a different file structure.appIt contains the files described below. These files are installed by angular-cli by default. app.module.ts - If you open the file, you will see that the code has reference todifferent libraries, which are imported. Angular-cli has used these default librariesfor the import – angular/core, platform-browser. The names itself explain the usageof the libraries.They are imported and saved into variables such as declarations, imports, providers,and bootstrap.import { BrowserModule } from '@angular/platform-browser';import { NgModule } from '@angular/core';import { AppComponent } from './app.component';@NgModule({declarations: [AppComponent],imports: [BrowserModule],providers: [],bootstrap: [AppComponent]})export class AppModule { }declarations - In declarations, the reference to the components is stored. TheAppcomponent is the default component that is created whenever a new project isinitiated. We will learn about creating new components in a different section.imports - This will have the modules imported as shown above. At present ,BrowserModule is part of the imports which is imported from @angular/platform-brow ser.providers - This will have reference to the services created. The service will be disc ussedin a subsequent chapter.bootstrap - This has reference to the default component created, i.e., AppComponent.20

Angular 4 app.component.css - You can write your css structure over here. Right now, wehave added the background color to the div as shown below.divdetails{background-color: #ccc;} app.component.html - The html code will be available in this file. !--The content below is only a placeholder and can be replaced. -- div class "divdetails" div style "text-align:center" h1 Welcome to {{title}}! /h1 img width "300"src "data:image/svg geG1sOnNwYWNlPSJwcmVzZ

About the Tutorial Angular 4 is a JavaScript framework for building web applications and apps in JavaScript, html, and TypeScript, which is a superset of JavaScript. Angular provides built-in features for animation, http service, and materials which in turn has feature

Related Documents:

it could be considered a new framework. Angular versions 2 and up are backward compatible till the Angular 2, but not with Angular 1. To avoid confusion, Angular 1 is now named Angu-lar JS and Angular versions 2 and higher are named Angular. Angular JS is based on JavaScript while Angular is based on JavaScript superset called TypeScript.

Angular Kinetics similar comparison between linear and angular kinematics Mass Moment of inertia Force Torque Momentum Angular momentum Newton’s Laws Newton’s Laws (angular analogs) Linear Angular resistance to angular motion (like linear motion) dependent on mass however, the more closely mass is distributed to the

Both Angular 2 and 4 are open-source, TypeScript-based front-end web application platforms. is the latest version of Angular. Although Angular 2 was a complete rewrite of AngularJS, there are no major differences between Angular 2 and Angular 4. Angular 4 is only an improvement and is backward compatible with Angular 2.

0. Since the angular measure q is the angular analog of the linear distance measure, it is natural to define the average angular velocity Xw\ as Xw\ q f-q 0 Dt where q 0 is the initial angular position of the object when Dt 0 and q f is the final angular position of the object after a time Dt of angular motion.

Here we consider the general formalism of angular momentum. We will discuss the various properties of the angular momentum operator including the commutation relations. The eigenvalues and eigenkets of the angular momentum are determined. The matrix elements of the angular momentum operators are evaluated for angular

Chapter 1: Getting started with Angular Remarks Angular (commonly referred to as "Angular 2 " or "Angular 2") is a TypeScript-based open-source front-end web framework led by the Angular Team at Google and by a community of individuals and corporations to address all of the parts of the developer's workflow while building complex web applications.

Angular JS Angular Course Curriculum Our Angular course content covers all Client-side technologies which is prepared by our Angular experts to make it suitable for everyone and ensure that it covers all the industry related skills and techniques to start a successful career growth. Our Experts always updating the Angular

Angular CLI is one of the most powerful accessibility tools available when developing apps with Angular. Angular CLI makes it easy to create an application and follows all the best practices! Angular CLI is a command-line interface tool that is used to initialize, develop, scaffold, maintain and even test and debug Angular applications.