Professional Documents
Culture Documents
CHAPTER 1
INTRODUCTION TO ANGULAR 4
Angular is a platform that makes it easy to build applications with the web.
Angular combines declarative templates, dependency injection, end to end tooling,
and integrated best practices to solve development challenges. Angular empowers
developers to build applications that live on the web, mobile, or the desktop.
ngIf
With the help of as keyword you can store the value as shown below −
</div>
The variable total stores the output of the slice using the as keyword.
Animation Package
TypeScript 2.2
Angular 4 has added a new pipe title case, which changes the first letter of
each word into uppercase.
<div>
</div>
The above line of code generates the following output – Angular 4 Titlecase.
Search parameters to the http get api is simplified. We do not need to call
URLSearchParams for the same as was being done in Angular2.
The scope is the binding part between the HTML (view) and the JavaScript
(controller).
The scope is an object with the available properties and methods.
The scope is available for both the view and the controller.
Angular is the blanket term used to refer to Angular 2, Angular 4 and all
other versions that come after AngularJS. Both Angular 2 and 4 are open-source,
TypeScript-based front-end web application platforms.
In Angular 4, the structural derivatives ngIf and ngFor have been improved,
and you can use if/else design syntax in your templates.
In this chapter, we will discuss the Environment Setup required for Angular
4.
Nodejs
Npm
Angular CLI
IDE for writing your code
Nodejs
C:\>node –v
v6.11.0
If it does not print anything, install nodejs on your system. To install nodejs,
go the homepage https://nodejs.org/en/download/ of nodejs and install the package
based on your OS.
Based on your OS, install the required package. Once nodejs is installed,
npm will also get installed along with it. To check if npm is installed or not, type
npm –v in the terminal. It should display the version of the npm.
C:\>npm –v
5.3.0
Angular 4 installations are very simple with the help of angular CLI. Visit
the homepage https://cli.angular.io/ of angular to get the reference of the
command.
Do you guys know if it's possible to add a module dependency after it's been
created? Something like that:
app/
components/
|__componentA/
|__app.js // initializes component A module
|__subcomponentA/
|__app.js // want to add subcomponentA dependencies to componentA module from here
Angular 4 stands on 8 building blocks following are the names and brief
explanation to it.
Modules: Modules are codes of line represented in blocks of code and they
perform various type of task. A module sends or does exports some sort of value in
the main code, such as class. When you do the study of Angular very foremost and
the most common module that you will see is the one that exports Component
Class.
Libraries: In Angular when we add "@" at the start it becomes ‘Libraries' in short
if we add prefix like this "@angular". Modules can also be a library of other
modules. If you see within Angular 2 or Angular 4 itself has many of the modules
that are libraries of others like @angular/core is the most important library that
contains most of the modules that we need.
Data binding: The main and powerful feature of JavaScript framework – Angular
2 or Angular 4 is data binding. Earlier version Angular 1 also supported data
binding so is the same with our new Angular 2 or Angular 4. There are 4 ways of
binding a data according to the direction to the DOM, from the DOM, or in both
directions:
<input [(ngModel)]="toy.name">
<li>{{toy.name}}</li>
<toy-detail [toy]="selectedToy"></toy-detail>
<li (click)="selectToy (toy)"></li>
TypeScript and Dart are language super sets of JavaScript. This means they
include all the features of JavaScript but add many helpful features as well. But
just like the ES6 version of JavaScript they don't work on current browsers. To
work around this limitation there are utilities to convert code written in their non-
browser-compliant syntax back to JavaScript ES5. In this way they provide the
developer with advanced features and still allow code to be run on existing
browsers. The process of converting code from one language to another is referred
to as 'transpilation'. Technically this conversion is performed by a language pre-
processor that, although it does not compile anything, is commonly referred to as a
compiler.
TypeScript is an open source language that is developed and maintained by
Microsoft. It provides type safety, advanced object oriented features, and
simplified module loading. Angular 4 itself is built using TypeScript. The
documentation support is better on the Angular 2 site for TypeScript than it is for
the other options. So although you could write your Angular 4 apps in pure ES5
JavaScript you may find it more difficult and find less support if you get into
trouble than if you bite the bullet and spends some time learning TypeScript.
Modules
Functions
Variables
Statements and Expressions
Comments TypeScript is a superset of JavaScript. That means any valid
JavaScript code is valid TypeScript code. But many prefer TypeScript because it
has additional features that we don’t have in the current version of JavaScript that
most browsers understand. So, when building Angular applications, we need to
have our TypeScript code converted into JavaScript code that browsers can
understand. This process is called transpilation which is the combination of
translate and compile. And that’s the job of the TypeScript compiler.
console.log(message)
console.log(message);
Line 2 prints the variable’s value to the prompt. Here, console refers to the
terminal window. The function log () is used to display text on the screen.
The type syntax for declaring a variable in TypeScript is to include a colon (:) after
the variable name, followed by its type. Just as in JavaScript, we use the var
keyword to declare a variable.
2. Declare its type but no value. In this case, the variable will be set to
undefined.
3. Declare its value but no type. The variable type will be set to any.
4. Declare neither value not type. In this case, the data type of the variable will
be any and will be initialized to undefined.
The following table illustrates the valid syntax for variable declaration as discussed
above –
console.log("name"+name)
console.log("name" + name);
Variables are scalar in nature. In other words, a variable declaration can only
contain a single at a time. This means that to store n values in a program n variable
declarations will be needed. Hence, the use of variables is not feasible when one
needs to store a larger collection of values.
Variables in a program are allocated memory in random order, thereby
making it difficult to retrieve/read the values in the order of their declaration.
Features of an Array
Syntax
Arrays may be declared and initialized in a single statement. The syntax for the
same is –
Creating classes
Use the class keyword to declare a class in TypeScript. The syntax for the same is
given below −
Syntax
class class_name {
//class scope
}
The class keyword is followed by the class name. The rules for identifiers must be
considered while naming a class.
Fields − A field is any variable declared in a class. Fields represent data pertaining
to objects
Constructors − Responsible for allocating memory for the objects of the class
Functions − Functions represent actions an object can take. They are also at times
referred to as methods
These components put together are termed as the data members of the class.
class Person {
function Person() {
return Person;
}());
class Car {
//field
engine:string;
//constructor
constructor(engine:string) {
this.engine = engine
//function
disp():void {
console.log("Engine is : "+this.engine)
The example declares a class Car. The class has a field named engine. The
var keyword is not used while declaring a field. The example above declares a
constructor for the class.
disp() is a simple function definition. Note that the function keyword is not
used here.
//constructor
function Car(engine) {
this.engine = engine;
//function
Car.prototype.disp = function () {
};
return Car;
}());
Object
An object is an instance which contains set of key value pairs. The values can be
scalar values or functions or even array of other objects. The syntax is given below
−
Syntax
var object_name = {
key1: “value1”, //scalar value
key2: “value”,
key3: function() {
//functions
},
key4:[“content1”, “content2”] //collection
};
var person = {
firstname:"Tom",
lastname:"Hanks"
};
console.log(person.firstname)
console.log(person.lastname)
2.8 Interfaces
CHAPTER 3
COMPONENTS IN ANGULAR 4
CHAPTER 4
<!doctype html>
<html ng-app>
<head>
<script src="http://code.angularjs.org/angular-
1.0.0rc10.min.js"></script>
</head>
<body>
<div>
<label>Name:</label>
<input type="text" ng-model="yourName" placeholder="Enter a name
here">
<hr>
<h1>Hello, {{yourName}}!</h1>
</div>
</body>
</html>
4.6 Binding Events
app.component.html
<!--The content below is only a placeholder and can be replaced.-->
<h1>
Welcome to {{title}}.
</h1>
</div>
<div> Months :
<select>
</select>
</div>
<br/>
<div>
Condition is valid.
</span>
</div>
<button (click)="myClickFunction($event)">
Click Me
</button>
(click)="myClickFunction($event)"
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
//array of months.
isavailable = true;
myClickFunction(event) {
//just added console.log which will display the event details in browser on
click of the button.
alert("Button is clicked");
console.log(event);
Upon clicking the button, the control will come to the function myClickFunction
and a dialog box will appear, which displays the Button is clicked as shown in the
following screenshot –
4.8 Setting Element Properties
CHAPTER 5
STRUCTURAL DIRECTIVES
With a template driven form, most of the work is done in the template; and
with the model driven form, most of the work is done in the component class.
Let us now consider working on the Template driven form. We will create a
simple login form and add the email id, password and submit the button in the
form. To start with, we need to import to FormsModule from @angular/core which
is done in app.module.ts as follows –
@NgModule({
declarations: [
SqrtPipe,
AppComponent,
NewCmpComponent,
ChangeTextDirective
],
imports: [
BrowserModule,
HttpModule,
FormsModule,
RouterModule.forRoot([
])
],
providers: [MyserviceService],
bootstrap: [AppComponent]
})
We have created a simple form with input tags having email id, password
and the submit button. We have assigned type, name, and placeholder to it.
In template driven forms, we need to create the model form controls by adding the
ngModel directive and the name attribute.
Thus, wherever we want Angular to access our data from forms, add
ngModel to that tag as shown above. Now, if we have to read the emailid and
passwd, we need to add the ngModel across it.
If you see, we have also added the ngForm to the #userlogin. The ngForm directive
needs to be added to the form template that we have created. We have also added
function onClickSubmit and assigned userlogin.value to it.
Let us now create the function in the app.component.ts and fetch the values
entered in the form.
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
todaydate;
componentproperty;
ngOnInit() {
this.todaydate = this.myservice.showTodayDate();
onClickSubmit(data) {
}
In the above app.component.ts file, we have defined the function
onClickSubmit. When you click on the form submit button, the control will come
to the above function.
The form looks like as shown below. Let us enter the data in it and in the
submit function, the email id is already entered.
7.18 Checkboxes
CHAPTER 8
CHAPTER 9
HTTP CLIENT
CHAPTER 10
CHAPTER 12