angular - 从子组件访问数据到父组件ngOnit Angular 4
问题描述
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
解决方案
推荐阅读
- reactjs - 打字稿:如何定义条件打字稿道具以访问 React 中的对象?
- python - 具有大时间线的 Seaborn xaxis
- python - MultiLineString 不适用于 Python 的 momepy.gdf_to_nx
- javascript - 嵌套导航器 - 反应原生
- reactjs - 如何为 PATCH API 调用编写单元测试
- td-engine - 如何在 tdengine 中创建纳米精度表
- windows - 为什么 msys2 bash 中的 win32 路径在遍历期间无法解析链接?
- reactjs - Next.js 路由与子路由的“next-connect”
- json - 如何在 Python 中将 JSON 格式转换为 XLSX 格式
- android - 在 App 中同时使用 Andorid AWS SDK 和 Amplify,Cognito 池问题?