私はこの単純なtypescriptですクラスがある:クラス算出特性
customer.ts
export class Customer {
public idCustomer: number;
public Code: string;
public BusinessName: string;
public sValue: any;
public sTitle: string;
constructor(values: Object = {}) {
Object.assign(this, values);
}
}
は、第3の特性(idCustomer、コード、BUSINESSNAME)はWEBAPI応答からまたはアプリケーションコードによって付加価値化されます。
私は何をしたいですこれら3つのプロパティの変更値、sValue ANS STITLEは、この式で更新されるたびその:
sValue = idCustomer
sTitle = BusinessName + ' (cod:' + Code + ')';
それを実現するために、クラスを変更するために、適切な方法は何ですか?
UPDATE 1
今、私が実装したGET/SET方法、と私は値
export class Customer {
//public idCustomer: number;
//public Code: string;
//public BusinessName: string;
private _idCustomer: number;
get idCustomer():number {
return this._idCustomer;
}
set idCustomer(newIdCustomer: number)
{
this._idCustomer = newIdCustomer;
this.sValue = this.idCustomer;
}
private _Code: string;
get Code(): string {
return this._Code;
}
set Code(newCode: string) {
this._Code = newCode;
this.sTitle = '(Cod:' + this._Code + ') ' + this._BusinessName;
alert();
}
private _BusinessName: string;
get BusinessName(): string {
return this._BusinessName;
}
set BusinessName(newBusinessName: string) {
this._BusinessName = newBusinessName;
this.sTitle = '(Cod:' + this._Code + ') ' + this._BusinessName;
}
public sValue: any;
public sTitle: string;
constructor(values: Object = {}) {
Object.assign(this, values);
}
}
しかし、この瞬間に
を新しいCustomerクラスをインスタンス化し、変更した場合、彼らは動作しますがから来た値でありますWebApiマッピング/購読は、sValueとsTitleが更新されない3つのプロパティ(idCustomer、Code、Business Name)のみを渡します...私はコンストラクタも変更する必要がありますが、それを行うには...
0あなたのプライベートメンバーの計算を確実にあなたのセッターを呼び出しますconstructor(values: { id: number, code: string, business: string }) {
this.idCustomer = values.id;
this.Code = values.code;
this.BusinessName = values.business;
}
:の
おかげ
メソッドまたはゲッターを作成します。 –
どうすればゲッターを作ることができますか? – DarioN1
さて、私は一歩前進しました。私は質問を統合します – DarioN1