2016-12-28 5 views
3

私はsimleサービスでコンポーネントを作った。 以下の動作を達成したい - サービスのhttp.get要求が完了したら、受け取ったパラメータを別のget要求に使用する。これは角2付き

ただし、Groupservice要求のthis.sections[0].Idパラメータは未定義であり、現在のものにはセクションパラメータがありません。 data.jsonから受け取ったデータは正しいですが。

コードは(追加輸入)

@Component({ 
    template: require('./content.component.html'), 
    styles: [require('./content.component.css')], 
    providers: [SectionService, GroupService] 
}) 
export class ContentComponent { 
    id: string; 
    sections: Array<Section>; 
    groups: Array<Group> 
    selectedSectionTitle: ""; 
    constructor(private router: Router, private activateRoute: ActivatedRoute, private sectionService: SectionService, private groupService: GroupService) { 
     this.router.events.subscribe(path => { 
      this.id = activateRoute.snapshot.params['id']; 
     }); 
     this.id = activateRoute.snapshot.params['id']; 

    } 


    ngOnInit() { 
     this.sectionService.getSections() 
      .subscribe((data: Response) => { 
       this.sections = data.json();   
      }, err => { 
       console.error("Error occurred while saving setting", err); 
      },() => { 
       this.groupService.getGroups(this.sections[0].Id).subscribe((data: Response) => { 
        this.groups = data.json(); 
       }) 
      }); 

    } 

} 

export class Group { 
    isSelectionComponentOpened: boolean; 
    isItemEditComponentOpened: boolean; 
    Id: string; 
    Title: string; 
    SectionId: string; 
    Items: Array<string> 
} 

export class Section { 
    Id: string; 
    Title: string; 

} 




@Injectable() 
export class SectionService { 


    constructor(private http: Http) { } 
    // Переделать на Observable 
    getSections() { 
     return this.http.get('Section/Get'); 
    } 
} 

私はこの問題の原因を把握することはできませんです。 お知らせください。

+0

これは角2です。申し訳ありません。 – Greenmachine

答えて

1

これは私が遭遇した奇妙なものだったサービス

+0

これは変数 '_self'に' this'の値を保持します。これはスコープ外にならないようにします – anshuVersatile

+0

OPはすでに太矢印の構文を使用しています。問題はスコープではなく、非同期操作についてです。 – echonax

+0

は、サービスのインスタンスを作成しないようにmodule.tsにのみ 'providers:[SectionService、GroupService]'を追加します。 – anshuVersatile

0

のインスタンスを避けるだろうmodule.tsでproviders: [SectionService, GroupService]を追加ngOnInit

@Component({ 
    template: require('./content.component.html'), 
    styles: [require('./content.component.css')], 

}) 
export class ContentComponent { 
    id: string; 
    sections: Array<Section>; 
    groups: Array<Group> 
    selectedSectionTitle: ""; 
    constructor(private router: Router, private activateRoute: ActivatedRoute, private sectionService: SectionService, private groupService: GroupService) { 
     this.router.events.subscribe(path => { 
      this.id = activateRoute.snapshot.params['id']; 
     }); 
     this.id = activateRoute.snapshot.params['id']; 

    } 


    ngOnInit() { 
     let _self = this; 
     this.sectionService.getSections() 
      .subscribe((data: Response) => { 
       _self.sections = data.json();   
      }, err => { 
       console.error("Error occurred while saving setting", err); 
      },() => { 
       this.groupService.getGroups(_self.sections[0].Id).subscribe((data: Response) => { 
        _self.groups = data.json(); 
       }) 
      }); 

    } 

} 

export class Group { 
    isSelectionComponentOpened: boolean; 
    isItemEditComponentOpened: boolean; 
    Id: string; 
    Title: string; 
    SectionId: string; 
    Items: Array<string> 
} 

export class Section { 
    Id: string; 
    Title: string; 

} 




@Injectable() 
export class SectionService { 


    constructor(private http: Http) { } 
    // Переделать на Observable 
    getSections() { 
     return this.http.get('Section/Get'); 
    } 
} 

として使用 let _self = this;。 セクションIDプロパティを小文字に変更すると、...

私はこの修正について説明しません。

関連する問題