2017-06-27 1 views
0

ここにオブジェクトがあります。 TypeModelオブジェクトとStatusModelオブジェクトには、1つの数値(たとえば1)でなければならない1つのプロパティtypeIDがあります。オブジェクトの配列内の1つのオブジェクトプロパティを変更すると、すべてのオブジェクトプロパティが変更されます(TypeScript)

export class TypeModel{ 
     typeID: number; 
     typeDescription: string; 
    } 


    export class StatusModel{ 
     typeId: number; 
     statusId: number; 
     typeStatusDescr: string; 
    } 

    export class TypeStatusesModel{ 
     type: TypeModel; 
     status: StatusModel; 
    } 


let typeStatuses: TypeStatusesModel[] = []; 
let typeStatusFirst: TypeStatusesModel= new TypeStatusesModel(); 
typeStatusFirst = { 
    type: { 
    typeID:1, 
    typeDescription: "Description Type 1" 
    }, 
    status: { 
    typeId: 1, 
    statusId: 1, 
    typeStatusDescr: "Description Status 1" 
    } 
} 
typeStatuses.push(typeStatusFirst); 

let typeStatusSecond: TypeStatusesModel= new TypeStatusesModel(); 
typeStatusSecond= { 
    type: { 
    typeID:2, 
    typeDescription: "Description Type 2" 
    }, 
    status: { 
    typeId: 1; 
    statusId: 2; 
    typeStatusDescr: "Description Status 2"; 
    } 
} 
typeStatuses.push(typeStatusSecond); 

私は私の最初のオブジェクトに同じプロパティ(typeID)を変更2に番号をtypeStatuses配列内の第二の目的のプロパティtypeIDを変更しようとすると問題があります。私は一時オブジェクトを作成し、 "壊れたもの"を置き換えようとしますが、何もしません。(let tempObject = Object.create(typeStatuses[1])

+1

これはほとんどありません。プロパティを変更するために使用しているコードを表示できますか?また、クラスを作成してから匿名オブジェクトを作成しています。クラスの代わりに、代わりにインターフェイスを使用する可能性があります。 – Rob

+0

そのコードに間違ったセミコロンがあります。タイプ:1。 ...型にする必要があります:1、... – kimy82

答えて

0

ちょうど昏睡状態になるはずのセミコロンが間違っています。私はまた、ちょうどインターフェイスのためのそれらのclasesを変更するでしょう。それはコンソールログ

で動作し

export class TypeModel{ 
     typeID: number; 
     typeDescription: string; 
    } 


    export class StatusModel{ 
     typeId: number; 
     statusId: number; 
     typeStatusDescr: string; 
    } 

    export class TypeStatusesModel{ 
     type: TypeModel; 
     status: StatusModel; 
    } 


let typeStatuses: TypeStatusesModel[] = []; 
let typeStatusFirst: TypeStatusesModel= new TypeStatusesModel(); 
typeStatusFirst = { 
    type: { 
    typeID:1, 
    typeDescription: "Description Type 1" 
    }, 
    status: { 
    typeId: 1, 
    statusId: 1, 
    typeStatusDescr: "Description Status 1" 
    } 
} 
typeStatuses.push(typeStatusFirst); 

let typeStatusSecond: TypeStatusesModel= new TypeStatusesModel(); 
typeStatusSecond= { 
    type: { 
    typeID:2, 
    typeDescription: "Description Type 2" 
    }, 
    status: { 
    typeId: 1, 
    statusId: 2, 
    typeStatusDescr: "Description Status 2", 
    } 
} 
typeStatuses.push(typeStatusSecond); 
typeStatuses[0].type.typeID=234; 
console.log(JSON.stringify(typeStatuses)); 

[{ "タイプ":{ "タイプID":234、 "typeDescription": "説明種類 1"}、 "ステータス":{ "typeId":1、 "statusId":1、 "typeStatusDescr": "説明 ステータス1"}}、{"type":{"typeID":2、 "typeDescription": "Description Type 2}}"ステータス ":{" typeId ":1、" statusId ":2、" typeS tatusDescr ":"説明 ステータス2 "}}]

だからコードは何かのようにすべきです。アウトサイドサイトのインターフェイスを使用しているのを見ることができないので、エクスポートを削除しました。

interface TypeModel{ 
     typeID: number; 
     typeDescription: string; 
    } 


    interface StatusModel{ 
     typeId: number; 
     statusId: number; 
     typeStatusDescr: string; 
    } 

    interface TypeStatusesModel{ 
     type: TypeModel; 
     status: StatusModel; 
    } 


let typeStatuses: TypeStatusesModel[] = []; 
let typeStatusFirst: TypeStatusesModel; 
typeStatusFirst = { 
    type: { 
    typeID:1, 
    typeDescription: "Description Type 1" 
    }, 
    status: { 
    typeId: 1, 
    statusId: 1, 
    typeStatusDescr: "Description Status 1" 
    } 
} 
typeStatuses.push(typeStatusFirst); 

let typeStatusSecond: TypeStatusesModel; 
typeStatusSecond= { 
    type: { 
    typeID:2, 
    typeDescription: "Description Type 2" 
    }, 
    status: { 
    typeId: 1, 
    statusId: 2, 
    typeStatusDescr: "Description Status 2", 
    } 
} 
typeStatuses.push(typeStatusSecond); 
typeStatuses[0].type.typeID=234; 
console.log(JSON.stringify(typeStatuses)); 
+0

それは実際のコードではないので、その間違いのため申し訳ありません、私はここに例を書いています。 – VSmolyanski

+1

@VSmolyanski例をここに掲載する前にテストしてください。それはあなたの問題を診断することが非常に難しい(特にあなたがそれが非常に起こりそうにないと主張しているので)。あなたの投稿には編集後も無効なセミコロンが含まれています。スニペットをテストし、修正して、実用的で再現性のある例を投稿に編集してください。 – Rob

関連する問題