【发布时间】:2017-03-28 10:23:03
【问题描述】:
我是 Angular 2 的新手。我正在尝试从 Angular 2 的 MVC 控制器获取数据
import { Component } from '@angular/core';
import { Http } from '@angular/http';
@Component({
selector: 'subscriber',
templateUrl: './subscriber.component.html',
styleUrls: ['./subscriber.component.css']
})
export class SubscriberComponent {
public IVM: IncidentViewModel;
constructor(private http: Http) {
//this.IVM.ID = "ID";
this.http.get('api/Form/IncidentForm').subscribe(res => this.IVM = res.json() as IncidentViewModel); //map
console.log(this.IVM.ID)
}
}
interface IncidentViewModel {
ID: string;
Code: string;
DateTime: Date;
Address: Address;
Contact: ContactViewModel;
}
interface ContactViewModel {
FirstName: string;
LastName: string;
Telephone1: string;
Telephone2: string;
}
interface Address {
street1: string;
}
这是我的控制器
namespace Subscriber.Controllers
{
[Route("api/[controller]")]
public class FormController : Controller
{
[HttpGet("[action]")]
public IncidentViewModel IncidentForm()
{
List<TimeLineViewModel> TLVM = new List<TimeLineViewModel>();
TLVM.Add(new TimeLineViewModel()
{
Date = DateTime.Now,
Notes = "notes 1",
Title = "n1"
});
TLVM.Add(new TimeLineViewModel()
{
Date = DateTime.Now.AddHours(1),
Notes = "notes 2",
Title = "n2"
});
TLVM.Add(new TimeLineViewModel()
{
Date = DateTime.Now.AddHours(2),
Notes = "notes 3",
Title = "n3"
});
TLVM.Add(new TimeLineViewModel()
{
Date = DateTime.Now.AddHours(3),
Notes = "notes 4",
Title = "n4"
});
IncidentViewModel IVM = new IncidentViewModel()
{
ID = Guid.NewGuid(),
Code = "12345678",
Date = DateTime.Now,
Contact = new ContactViewModel()
{
FirstName = "kostas",
LastName = "kostas",
Telephone1 = "6974123456",
Telephone2 = "2101234567"
},
Address = new AddressViewModel()
{
Street1 = "asdf 9"
},
TimeLine = TLVM
};
return IVM;
}
}
}
我无法将数据从控制器绑定到 angular2 组件。当我尝试仅在控制台内显示时(console.log(this.IVM.ID))我收到一个未识别的错误,所以我认为问题出在订阅内部。有什么建议吗?
【问题讨论】:
标签: angular model-view-controller asp.net-core http-get