2016-12-28 1 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 속성을 소문자로 변경하면 ...

이 수정에 대한 설명이 없습니다.

관련 문제