前提・実現したいこと
Angularを使って、画面を作成しています。
Serviceでgetしたオブジェクトを、componetnに通知し、componentで成形しています。
成形のなかで、別オブジェクトへ代入しているのですが、その際に中身は入っているように見えるのですが、lengthをとるとundifinedになってしまいます。
下記にコードを記載いたしますので、ご教授お願いいたします。
該当のソースコード
Component
typescript
1export class MyComponent() { 2 private _testList: TestClass[]; 3 private _testAll: TestAll; 4 5 ngOnInit() { 6 this._testList = service.getTestList(); 7 } 8 9 onSubmit() { 10 this._testAll.ids = this._testList; 11 this._testAll.comment = 'fugafuga'; 12 13 console.log(this._testAll.ids) // 0:{id: 0, name: "test0"}... 14 console.log(this._testAll.ids.length) // undifined 15 } 16}
Service
typescript
1 2export class MyService() { 3 private testList: TestID[]; 4 5 getTestList() { 6 this.testList = new Array(10); 7 for (i = 0; i < 10; i++) { 8 var test: TestID; 9 test.id = i; 10 test.name = 'testUser' + i.toString(); 11 12 this.testList[i] = test; 13 } 14 15 return this.testList; 16}
Classオブジェクト
typescript
1export class TestID { 2 id: number; 3 name: String; 4} 5 6export class TestAll { 7 ids: TestID[]; 8 comment: String; 9 hogehoge: String;
Componentのコンソールに表示しているところで、lengthがundefinedになっている理由が分かりません。
補足情報(FW/ツールのバージョンなど)
Angular: 5.1.2
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー