Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Angular UI Development with PrimeNG

You're reading from   Angular UI Development with PrimeNG Build rich UI for Angular applications using PrimeNG

Arrow left icon
Product type Paperback
Published in Jul 2017
Publisher Packt
ISBN-13 9781788299572
Length 384 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Sudheer Jonna Sudheer Jonna
Author Profile Icon Sudheer Jonna
Sudheer Jonna
Oleg Varaksin Oleg Varaksin
Author Profile Icon Oleg Varaksin
Oleg Varaksin
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Getting Started with Angular and PrimeNG 2. Theming Concepts and Layouts FREE CHAPTER 3. Enhanced Inputs and Selects 4. Button and Panel Components 5. Data Iteration Components 6. Amazing Overlays and Messages 7. Endless Menu Variations 8. Creating Charts and Maps 9. Miscellaneous Use Cases and Best Practices 10. Creating Robust Applications

Interfaces, classes, and enums

An interface is a way to take a particular structure/shape and give it a name so that we can reference it later as a type. It defines a contract within our code. Interfaces begin with the keyword interface. Let's take an example:

interface Person {
name: string
children?: number
isMarried(): boolean
(): void
}

The specified interface Person has the following:

  • The name property of type string.
  • The optional property children of type number. Optional properties are denoted by a question mark and can be omitted.
  • The isMarried method that returns a boolean value.
  • Anonymous (unnamed) method that returns nothing.

Typescript allows you to use the syntax [index: type] to specify a string or number type based collection of key/value pairs. Interfaces perfectly fit such data structures. For example, consider the following syntax:

interface Dictionary {
[index: number]: string
}
An interface is only used by TypeScript compiler at compile time, and is then removed. Interfaces don't end up in the final JavaScript output. General, no types appear in the output. You can see that in the TypeScript playground mentioned earlier.

Beside interfaces, there are classes that describe objects. A class acts as a template for instantiating specific objects. The syntax for TypeScript's classes is almost identical to that of native classes in ECMAScript 2015 with some handy additions. In TypeScript, you can use public, private, protected, and readonly access modifiers:

class Dog {
private name: string; // can only be accessed within this class
readonly owner: string = "Max"; // can not be modified
constructor(name: string) {this.name = name;}
protected sayBark() { }
}

let dog = new Dog("Sam");
dog.sayBark(); // compiler error because method 'sayBark' is protected and
// only accessible within class 'Dog' and its subclasses.

Members with omitted modifiers are public by default. If a property or method is declared with the static keyword, there is no need to create an instance to access them.

A class can be abstract, that means, it may not be instantiated directly. Abstract classes begin with the keyword abstract. A class can implement an interface as well as extend another class. We can achieve that using the implements and extends keywords, respectively. If a class implements some interface, it must adopt all properties from this interface; otherwise, you will get an error about missing properties:

interface Animal {
name: string;
}

class Dog implements Animal {
name: string;
// do specific things
}

class Sheepdog extends Dog {
// do specific things
}
Derived classes that contain constructor functions must call super(). The super() call executes the constructor function on the base class.

It is possible to declare a constructor parameter with a modifier. As result, a member will be created and initialized in one place:

class Dog {
constructor(private name: string) { }

// you can now access the property name by this.name
}
This shortened syntax is often used in Angular when we inject services into components. Angular's services are normally declared in the component's constructor with the private modifier.

The last basic type to be mentioned here is enum. Enums allow us to define a set of named constants. Enum members have numeric values associated with them (started with 0):

enum Color {
Red,
Green,
Blue
}

var color = Color.Red; // color has value 0
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime