A similar project returning data from a json file displayed the data
correctly. I'm trying to translate it to return data from a ASP.NET 5
WebAPI Controller. I inserted a breakpoint on the Controller and it didn't
appear to be calling to Controller.
*app.component.ts*
import { Inject, Component } from 'angular2/core';
import { DataService } from './data.service';
import { HTTP_BINDINGS } from 'angular2/http';
@Component({
selector: 'app',
bindings: [DataService, HTTP_BINDINGS],
template: `<h1>Hello World</h1>
<li *ngFor="#item of items">
<span>{{item.Name}}</span>
</li>
`
})
export class AppComponent {
private items: any[];
constructor( @Inject(DataService) public dataService: DataService) {
this.dataService = dataService;
}
ngOnInit() {
this.dataService.getItems()
.subscribe((items: any[]) => {
this.items = items;
});
}
}
*data.service.ts*
import { Injectable } from 'angular2/core';
import { Http, Response } from 'angular2/http';
import 'rxjs/add/operator/map';
@Injectable()
export class DataService {
private http: Http;
constructor(http: Http) {
this.http = http;
}
getItems() {
return this.http.get('/api/todo')
.map((res: Response) => res.json());
}
}
*Web Api Controller*
[HttpGet]
public IEnumerable<TodoItem> GetAll()
{
return TodoItems.GetAll().OrderBy(x=>x.Name);
}
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.
import { Inject, Component } from 'angular2/core';
import { DataService } from './data.service';
import { HTTP_BINDINGS } from 'angular2/http';
@Component({
selector: 'app',
bindings: [DataService, HTTP_BINDINGS],
template: `<h1>Hello World</h1>
<li *ngFor="#item of items">
<span>{{item.Name}}</span>
</li>
`
})
export class AppComponent {
private items: any[];
constructor( @Inject(DataService) public dataService: DataService) {
this.dataService = dataService;
}
ngOnInit() {
this.dataService.getItems()
.subscribe((TodoItemsitems: any[]) => {
this.items = TodoItems;
});
}
}
import { Injectable } from 'angular2/core';
import { Http, Response } from 'angular2/http';
import 'rxjs/add/operator/map';
@Injectable()
export class DataService {
private http: Http;
constructor(http: Http) {
this.http = http;
}
getItems() {
return this.http.get('/api/todo')
.map((res: Response) => res.json());
}
}
using Microsoft.AspNet.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TodoApi.Models;
namespace TodoApi.Controllers
{
[Route("api/[controller]")]
public class TodoController : Controller
{
[FromServices]
public ITodoRepository TodoItems { get; set; }
[HttpGet]
public IEnumerable<TodoItem> GetAll()
{
return TodoItems.GetAll().OrderBy(x=>x.Name);
}
[HttpGet("{id}", Name = "GetTodo")]
public IActionResult GetById(string id)
{
var item = TodoItems.Find(id);
if (item == null)
{
return HttpNotFound();
}
return new ObjectResult(item);
}
[HttpPost]
public IActionResult Create([FromBody] TodoItem item)
{
if (item == null)
{
return HttpBadRequest();
}
TodoItems.Add(item);
return CreatedAtRoute("GetTodo", new { controller = "Todo", id = item.Key }, item);
}
[HttpPut("{id}")]
public IActionResult Update(string id, [FromBody] TodoItem item)
{
if (item == null || item.Key != id)
{
return HttpBadRequest();
}
var todo = TodoItems.Find(id);
if (todo == null)
{
return HttpNotFound();
}
TodoItems.Update(item);
return new NoContentResult();
}
[HttpDelete("{id}")]
public void Delete(string id)
{
TodoItems.Remove(id);
}
}
}