mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-25 02:57:31 +00:00
Add data fetching example for Angular 2 template
This commit is contained in:
@@ -1,3 +0,0 @@
|
||||
<h2>About</h2>
|
||||
|
||||
<p>Use this area to provide additional information.</p>
|
||||
@@ -1,10 +0,0 @@
|
||||
import * as ng from 'angular2/core';
|
||||
|
||||
@ng.Component({
|
||||
selector: 'about'
|
||||
})
|
||||
@ng.View({
|
||||
template: require('./about.html')
|
||||
})
|
||||
export class About {
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
import * as ng from 'angular2/core';
|
||||
import * as router from 'angular2/router';
|
||||
import { Http, HTTP_BINDINGS } from 'angular2/http';
|
||||
import { NavMenu } from '../nav-menu/nav-menu.ts';
|
||||
import { Home } from '../home/home.ts';
|
||||
import { About } from '../about/about';
|
||||
import { NavMenu } from '../nav-menu/nav-menu';
|
||||
import { Home } from '../home/home';
|
||||
import { FetchData } from '../fetch-data/fetch-data';
|
||||
import { Counter } from '../counter/counter';
|
||||
|
||||
@ng.Component({
|
||||
@@ -11,8 +11,8 @@ import { Counter } from '../counter/counter';
|
||||
})
|
||||
@router.RouteConfig([
|
||||
{ path: '/', component: Home, name: 'Home' },
|
||||
{ path: '/about', component: About, name: 'About' },
|
||||
{ path: '/counter', component: Counter, name: 'Counter' }
|
||||
{ path: '/counter', component: Counter, name: 'Counter' },
|
||||
{ path: '/fetch-data', component: FetchData, name: 'FetchData' }
|
||||
])
|
||||
@ng.View({
|
||||
template: require('./app.html'),
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
<h1>Weather forecast</h1>
|
||||
|
||||
<p>This component demonstrates fetching data from the server.</p>
|
||||
|
||||
<p *ngIf="!forecasts"><em>Loading...</em></p>
|
||||
|
||||
<table class='table' *ngIf="forecasts">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Date</th>
|
||||
<th>Temp. (C)</th>
|
||||
<th>Temp. (F)</th>
|
||||
<th>Summary</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr *ngFor="#forecast of forecasts">
|
||||
<td>{{ forecast.dateFormatted }}</td>
|
||||
<td>{{ forecast.temperatureC }}</td>
|
||||
<td>{{ forecast.temperatureF }}</td>
|
||||
<td>{{ forecast.summary }}</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
@@ -0,0 +1,27 @@
|
||||
import * as ng from 'angular2/core';
|
||||
import fetch from 'isomorphic-fetch';
|
||||
|
||||
@ng.Component({
|
||||
selector: 'fetch-data'
|
||||
})
|
||||
@ng.View({
|
||||
template: require('./fetch-data.html')
|
||||
})
|
||||
export class FetchData {
|
||||
public forecasts: WeatherForecast[];
|
||||
|
||||
constructor() {
|
||||
fetch('/api/SampleData/WeatherForecasts')
|
||||
.then(response => response.json())
|
||||
.then((data: WeatherForecast[]) => {
|
||||
this.forecasts = data;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
interface WeatherForecast {
|
||||
dateFormatted: string;
|
||||
temperatureC: number;
|
||||
temperatureF: number;
|
||||
summary: string;
|
||||
}
|
||||
@@ -23,7 +23,7 @@
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a [routerLink]="['/About']">
|
||||
<a [routerLink]="['/FetchData']">
|
||||
<span class='glyphicon glyphicon-th-list'></span> Fetch data
|
||||
</a>
|
||||
</li>
|
||||
|
||||
Reference in New Issue
Block a user