2017-03-28 2 views
0

私は asp.netコアAngular2 MVC HTTP GET私は、角2に新しいです問題

2角度
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; 
} 

からMVCコントローラからデータを取得しようとしています、これは私のコントローラである

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 = "21" 
       }, 
       Address = new AddressViewModel() 
       { 
        Street1 = "asdf 9" 
       }, 
       TimeLine = TLVM 
      }; 
      return IVM; 
     } 
    } 
} 

私はコントローラーからangle2コンポーネントにデータをバインドできません。私はちょうどコンソール(console.log(this.IVM.ID))の中に表示しようとすると、私は問題が不安定になるので、問題が購読中であると思う。助言がありますか?

答えて

0

getがより次のようになります。

getHeroes(): Observable<Hero[]> { 
    return this.http.get(this.heroesUrl) 
        .map(this.extractData) 
        .catch(this.handleError); 
} 

は完全例えばdocsを参照してください。

関連する問題