Skip to content

Commit 6d2e51b

Browse files
Add data fetching example for Angular 2 template
1 parent 97ac684 commit 6d2e51b

12 files changed

Lines changed: 230 additions & 20 deletions

File tree

templates/Angular2Spa/ClientApp/components/about/about.html

Lines changed: 0 additions & 3 deletions
This file was deleted.

templates/Angular2Spa/ClientApp/components/about/about.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

templates/Angular2Spa/ClientApp/components/app/app.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import * as ng from 'angular2/core';
22
import * as router from 'angular2/router';
33
import { Http, HTTP_BINDINGS } from 'angular2/http';
4-
import { NavMenu } from '../nav-menu/nav-menu.ts';
5-
import { Home } from '../home/home.ts';
6-
import { About } from '../about/about';
4+
import { NavMenu } from '../nav-menu/nav-menu';
5+
import { Home } from '../home/home';
6+
import { FetchData } from '../fetch-data/fetch-data';
77
import { Counter } from '../counter/counter';
88

99
@ng.Component({
1010
selector: 'app'
1111
})
1212
@router.RouteConfig([
1313
{ path: '/', component: Home, name: 'Home' },
14-
{ path: '/about', component: About, name: 'About' },
15-
{ path: '/counter', component: Counter, name: 'Counter' }
14+
{ path: '/counter', component: Counter, name: 'Counter' },
15+
{ path: '/fetch-data', component: FetchData, name: 'FetchData' }
1616
])
1717
@ng.View({
1818
template: require('./app.html'),
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<h1>Weather forecast</h1>
2+
3+
<p>This component demonstrates fetching data from the server.</p>
4+
5+
<p *ngIf="!forecasts"><em>Loading...</em></p>
6+
7+
<table class='table' *ngIf="forecasts">
8+
<thead>
9+
<tr>
10+
<th>Date</th>
11+
<th>Temp. (C)</th>
12+
<th>Temp. (F)</th>
13+
<th>Summary</th>
14+
</tr>
15+
</thead>
16+
<tbody>
17+
<tr *ngFor="#forecast of forecasts">
18+
<td>{{ forecast.dateFormatted }}</td>
19+
<td>{{ forecast.temperatureC }}</td>
20+
<td>{{ forecast.temperatureF }}</td>
21+
<td>{{ forecast.summary }}</td>
22+
</tr>
23+
</tbody>
24+
</table>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as ng from 'angular2/core';
2+
import fetch from 'isomorphic-fetch';
3+
4+
@ng.Component({
5+
selector: 'fetch-data'
6+
})
7+
@ng.View({
8+
template: require('./fetch-data.html')
9+
})
10+
export class FetchData {
11+
public forecasts: WeatherForecast[];
12+
13+
constructor() {
14+
fetch('/api/SampleData/WeatherForecasts')
15+
.then(response => response.json())
16+
.then((data: WeatherForecast[]) => {
17+
this.forecasts = data;
18+
});
19+
}
20+
}
21+
22+
interface WeatherForecast {
23+
dateFormatted: string;
24+
temperatureC: number;
25+
temperatureF: number;
26+
summary: string;
27+
}

templates/Angular2Spa/ClientApp/components/nav-menu/nav-menu.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</a>
2424
</li>
2525
<li>
26-
<a [routerLink]="['/About']">
26+
<a [routerLink]="['/FetchData']">
2727
<span class='glyphicon glyphicon-th-list'></span> Fetch data
2828
</a>
2929
</li>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNet.Mvc;
6+
7+
namespace WebApplicationBasic.Controllers
8+
{
9+
[Route("api/[controller]")]
10+
public class SampleDataController : Controller
11+
{
12+
private static string[] Summaries = new[]
13+
{
14+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
15+
};
16+
17+
[HttpGet, Route("[action]")]
18+
public IEnumerable<WeatherForecast> WeatherForecasts()
19+
{
20+
var rng = new Random();
21+
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
22+
{
23+
DateFormatted = DateTime.Now.AddDays(index).ToString("d"),
24+
TemperatureC = rng.Next(-20, 55),
25+
Summary = Summaries[rng.Next(Summaries.Length)]
26+
});
27+
}
28+
29+
public class WeatherForecast
30+
{
31+
public string DateFormatted { get; set; }
32+
public int TemperatureC { get; set; }
33+
public string Summary { get; set; }
34+
35+
public int TemperatureF
36+
{
37+
get
38+
{
39+
return 32 + (int)(this.TemperatureC / 0.5556);
40+
}
41+
}
42+
}
43+
}
44+
}

templates/Angular2Spa/Startup.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.Extensions.Configuration;
99
using Microsoft.Extensions.DependencyInjection;
1010
using Microsoft.Extensions.Logging;
11+
using Newtonsoft.Json.Serialization;
1112

1213
namespace WebApplicationBasic
1314
{
@@ -28,7 +29,10 @@ public Startup(IHostingEnvironment env)
2829
public void ConfigureServices(IServiceCollection services)
2930
{
3031
// Add framework services.
31-
services.AddMvc();
32+
services.AddMvc().AddJsonOptions(options =>
33+
{
34+
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
35+
});
3236
}
3337

3438
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

templates/Angular2Spa/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"babel-core": "^6.5.2",
2929
"es6-shim": "^0.33.13",
3030
"es7-reflect-metadata": "^1.5.5",
31+
"isomorphic-fetch": "^2.2.1",
3132
"reflect-metadata": "^0.1.2",
3233
"rxjs": "^5.0.0-beta.2",
3334
"zone.js": "^0.5.15"

templates/Angular2Spa/tsd.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
"installed": {
88
"requirejs/require.d.ts": {
99
"commit": "dade4414712ce84e3c63393f1aae407e9e7e6af7"
10+
},
11+
"isomorphic-fetch/isomorphic-fetch.d.ts": {
12+
"commit": "4d2d0653003a4d1df4d7c68054cce4f4ace58bdf"
1013
}
1114
}
1215
}

0 commit comments

Comments
 (0)