Creating a New Angular Project
Open Angular IDE, click File
in the top menu, then select,New
then click Angular Project
Angular IDE will take a few minutes to set up the project, you can see the activities output in Terminal+. As shown below
Once the project is created, we can now install PrimeNG and Angular CDK libraries to our project. In the opened terminal+ type the following commands.
npm install primeng --save
npm install @angular/cdk --save
Now, configure required styles at the styles section angular.json
"styles": [
"node_modules/primeng/resources/themes/nova-light/theme.css",
"node_modules/primeng/resources/primeng.min.css",
"src/styles.css"
]
Run Application
Run the application from the server tab of Angular IDE. Run the application from the server tab of Angular IDE. With Live Preview 2.0, you get an immediate preview of the application right in the IDE.
Now, you get the immediate preview of the Angular application in Angular IDE
Choosing an API
The CoinMarketCap API provides you the world’s Cryptocurrency data. To get a top 10 cryptocurrency list and their data, CoinMarketCap API provides us an ordered list on https://api.coinmarketcap.com/v1/ticker/?limit=10, we can open this URL in a browser to see the list of the data that is going to be returned.
To store the data returned, let us create a TypeScript class file named coin.ts
in the app folder, with the following content.
export class Coin {
id: string;
name: string;
symbol: string;
rank: string;
priceUsd: string;
constructor(attrs: any = null) {
if (attrs) {
this.build(attrs);
}
}
build(attrs: any) {
this.id = attrs.id;
this.name = attrs.name;
this.symbol = attrs.symbol;
this.rank = attrs.rank;
this.priceUsd = attrs.price_usd;
}
}
Building the App
We’ll use DataView
component. DataView displays data in a grid or list layout with pagination, sorting and filtering features. Let’s create a new service called crypto
In the project explorer view, right-click on app
, select new
, and click Service
and as shown below:
Services are one of the fundamental blocks of every Angular application. They are used when dealing with APIs.
Replace the code of crypto.service.ts
as below:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class CryptoService {
constructor(private http: HttpClient) { }
list(): Observable {
return this.http
.get('https://api.coinmarketcap.com/v1/ticker/?limit=10')
.pipe(map(data => data));
}
}
We just defined a simple service and a method called list
which return the top 10 coins from CoinMarketCap API.
Now, open your `app.module.ts` file and Import DataView
component:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { DataViewModule } from 'primeng/dataview';
import { HttpClientModule } from '@angular/common/http';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
DataViewModule,
HttpClientModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
In the code snippet above, we also imported HttpClientModule
for crypto
service created earlier.
Now, update the code of `app.component.ts` file with the code provided below:
import { Component } from '@angular/core';
import { CryptoService } from './crypto.service';
import { Coin } from './coin';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
coins: Coin[];
constructor(private service: CryptoService) {
this.loadCoins();
}
loadCoins() {
this.service.list().subscribe(
coins => {
this.coins = coins.map(c => new Coin(c));
}
);
}
}
The code above fires crypto
service injected by dependency.
Now, we can use DataView
component which requires a collection of items as its value and one or more templates depending on the layout mode:
{{coin.name}}
{{coin.name}}
New Live Preview browser pane in Angular IDE
You can now customize your template as you want, layout options selector, sorting and filtering are baked-in and no strict UI is enforced to make it possible to come up with your own UI elements to enable these features.