2017-03-29 12 views
1

私はこのように自分のドメインクラスを使用しています:活字体の下線ケース翻訳

export class Contact { 

    private _name: string; 
    private _phone: string; 

    get name(): string { 
    return this._name; 
    } 

    set name(value: string) { 
    this._name = value; 
    } 

    get phone(): string { 
    return this._phone; 
    } 

    set phone(value: string) { 
    this._phone = value; 
    } 

} 

私は角http.postを通じてバックエンドにデータを送信するときに私の問題があり、送信されたプロパティはアンダースコアを持つものと私ですバックエンドはcamelCaseのためだけに用意されています(変更バックエンドはここでは選択できません)。送信前にJSONを翻訳する以外にも、他に知られているオプションはありますか? ありがとうございます。

答えて

3

あなたはそれを行うには、2つの方法があります。

let serialized = JSON.stringify(contact, Object.keys(contact.constructor.prototype)) 
:第二引数を置き換え、 JSON.stringify(...)を使用することにより

public toJSON() { 
    return { 
     name: this._name, 
     phone: this._phone, 
    }; 
} 

2):

1)あなたのContactクラスのtoJSON()機能を定義します

詳しくはhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify