从子组件访问数据到父组件ngOnit Angular 4

时间:2018-08-18 10:28:03

标签: angular angular-httpclient

在控制台中打印时,

selectedgoalId从子组件正确获取。 但是当传递给url时,它显示为未定义

export class SdgComponent implements OnInit {
    categoryId: number;
    selectedgoalId: number;
    monthId: string = 'July';
    fulldata: sdgdatamodel[];
    servicestatus: string = 'data loading please wait';

    constructor(private _route: ActivatedRoute, private _sdgdataservice:sdgdataservice) { }

    ngOnInit() {
        this._route.paramMap.subscribe(params => {
            this.categoryId = +params.get('categoryid');
            this.selectedgoalId = this.selectedgoalId;
            this._sdgdataservice.Getdata(this.categoryId, this.selectedgoalId, this.monthId)
                .subscribe(data => this.fulldata = data,
                    (error) => {
                        this.servicestatus = error;
                    }
                );
        });
    }

    ongoalchange(goalId): void {
        this.selectedgoalId = goalId;  //this value coming from child component
        console.log(goalId);           //value getting correctly
    }
}

以下是服务

@Injectable()
export class sdgdataservice {
    constructor(private _http: Http) { }
    Getdata(catId, selectedgoalId, MonthId): Observable<sdgdatamodel[]> {
        return this._http.get('http://localhost/dummy/data.php?category_id=' + catId + '&goal_id=' + selectedgoalId + '&month_id=' + MonthId)
            .pipe(
                map((res: Response) => res.json()),
                catchError(this.handleError)
            );
    }
    handleError(error: HttpErrorResponse) {
        console.log(error);
        return throwError(error.message);
    }
}

以下是我得到的错误

http://localhost/dummy/data.php?category_id=1&goal_id=undefined&month_id=July

0 个答案:

没有答案