2016-12-09 8 views
1

`こんにちは、 Visual Studio 2015 .net Core、Angular 2 & TypescriptのWeb APIからJSONデータを取得するにはいくつかの助けが必要です。/wwwroot/libsのAngular2フォルダ。Jsonオブジェクトの角2とTypescriptを使ってJsonオブジェクトを取得する

私はAngular 2のhttp.get()を使用しています。 get()関数の結果は、未定義の値を表示します...なぜですか?

そこが私のファイルです:

Folders

[API] [2]

CustomersController.cs =>アクセスOK経由http://localhost:45149/api/customers/ => [{...}、{ ...}、{...}]

public class CustomersController : Controller 
{ 
    // GET: api/customers 
    [HttpGet] 
    public IList<Customer> Get() 
    { 
     var objects = new List<Customer> 
     { 
      new Customer { Id = 1, Log = "user01", Name = "user1" }, 
      new Customer { Id = 2, Log = "user02", Name = "user2" }, 
      new Customer { Id = 3, Log = "user03", Name = "user3" } 
     }; 
     return objects; 
    } 
} 

customer.ts

export class Customer { 
constructor(
    public Id: number, 
    public Log: string, 
    public Name: string 
    ){} 
} 

customer.service.ts

import { Injectable } from "@angular/core"; 
import { Http } from "@angular/http"; 
import { Observable } from "rxjs/Observable"; 
import { Customer } from "./customer"; 
import "rxjs/Rx" 

@Injectable() 
export class CustomerService { 
private baseUrl = "http://localhost:45149/api/customers/"; // web api URL 
constructor(private http: Http) { } 

getCustomers() { 
    return this.http.get(this.baseUrl) 
     .map(res => <Customer[]> res.json()) 
     .catch(error => { 
      console.log(error); 
      return Observable.throw(error); 
     }); 
}} 

customer.component.ts

import { Component, OnInit } from '@angular/core'; 
import { HTTP_PROVIDERS } from '@angular/http'; 
import { Customer } from './customer'; 
import { CustomerService } from './customer.service' 

@Component({ 
selector: 'app-api', 
template: ` 
<ul class=""> 
<li *ngFor="let customer of customers"> 
<span class=""> 
{{customer.Id}} </span> {{customer.Name}} 
</li> 
</ul>`, 
    providers: [ 
            HTTP_PROVIDERS, 
            CustomerService 
    ] }) 

    export class CustomerComponent implements OnInit{ 
    api: string; 
    public customers: Customer[]; 

    constructor(private customerService: CustomerService) { 
    this.api = "API Customers"; 
} 

ngOnInit() { 
    this.customerService.getCustomers() 
     .subscribe(
     customers => 
     { 
      console.log(this.customers); 
      this.customers = customers; 
     }, 
     error => alert("error")); 
}} 

app.module.ts

// 
*** Imports *** 
// 

@NgModule({ 
imports: [BrowserModule, routing], //other modules the app depends on 
providers: [CustomerService], 
declarations: [AppComponent, 
HomeComponent,UploadComponent,CustomerComponent], 
bootstrap: [AppComponent] // root component to bootstarp 
}) 

export class AppModule { } 

app.component.ts

import { Component } from '@angular/core'; 

@Component({ 
selector: 'app-root', 
templateUrl: './app/app.component.html' // => SPA 
}) 

export class AppComponent { 
title = "App-root" 
} 

ありがとうございました。

+1

上の小文字の名前に結合することによって、問題ないはず? ngModuleで "HttpModule"をインポートし、 "HTTP_PROVIDERS"をリムーバブルにしてみてください –

+0

ネットワークタブでリクエストを取得できません(要素を調べる=>ネットワーク)...すべて私のHttpの応答です()はURLに到達できますが、値の中の何も=>未定義です。顧客の価値を顧客に初期化すると、Customer [] = [];値は "array [0] – amin89

+1

です。log文は現在this.customersへの代入の前です。代入後にlog文を置くと何が記録されますか? – hagner

答えて

0

大文字でプロパティにバインドしようとしているときに、あなたのサーバーがcamel-casingで応答をシリアル化するように見えます。

シリアライザの設定を別の形式に変更する場合は、次の記事を参照してください。Web API serialize properties starting from lowercase letter

そうしないと、あなたは、ブラウザ上で開発者向けツールで、ネットワークタブの「GET」リクエストを見ることができる角度側

+0

サーバサイドクラス(api)に関連するクラスまたはインターフェイスを作成することは必須ですか?そうでない場合は、なぜそれを実装するのですか? ty – amin89

+0

表示したいだけのクラスを作成することは必須ではありません。あなたがしたい場合は、変数anyをタイプして取得し、次にテンプレートの正しいプロパティにバインドするだけの変数を使用することができます。しかし、コンポーネントやディレクティブで何らかの方法でやりとりしたいと思うとすぐに、ソート、フィルタリングなどのことをするには、そのクラスが必要です。 – hagner

関連する問題