親コンポーネントから子コンポーネントを継承しています。Angular4親コンポーネントの変数から子コンポーネントの変数を割り当てる方法を定義していないことを確認してから
親コンポーネントが、APIコンポーネントのリクエストを行い、子コンポーネントにデータを渡しています。
親コンポーネント:
export class ChartComponent implements OnInit {
@Input()
protected exercisesData: any[];
@Input()
protected weightData: any[];
/**
*
*/
constructor(public dataService: DataService) {
}
ngOnInit(): void {
this.dataService.setEndpoint('/api/dashboard/get');
this.get();
}
private get() {
this.dataService
.Get()
.subscribe(data => {
this.exercisesData = Object.assign({}, data);
this.weightData = Object.assign({}, data);
console.log(this.exercisesData);
console.log(this.weightData);
}, (error) => {
console.log(error);
});
}
}
子コンポーネント:子コンポーネントの
export class ExercisesProgressChartComponent extends ChartComponent{
private filterableData: any[];
private dropDownSelectedValue: string;
ngOnInit(): void {
this.dropDownSelectedValue = "0";
this.filterableData = Object.assign({}, this.exercisesData);
}
private onDropDownChange(dropDownSelectedValue) {
if(dropDownSelectedValue == "0"){
this.filterableData = Object.assign({}, this.exercisesData);
}
else{
this.filterableData = Object.assign({}, this.exercisesData);
this.filterableData["exercisesProgress"] = this.filterableData["exercisesProgress"].filter(x=>x.id == dropDownSelectedValue);
}
}
}
は、テンプレート:
<div *ngIf="exercisesData" class="dashboard">
<div class="form-group">
<select class="form-control" [(ngModel)]="dropDownSelectedValue" (ngModelChange)="onDropDownChange($event)" sele>
<option [value]="0">All Exercises</option>
<option *ngFor="let x of exercisesData.exercisesProgress" [value]="x.id">{{x.name}}</option>
</select>
<small id="exerciseNameHelp" class="form-text text-muted">Please select exercise for filtering</small>
</div>
{{exercisesData.exercisesProgress | json}}
{{filterableData.exercisesProgress | json}}
<ngx-charts-line-chart
[scheme]="{domain: ['#5AA454', '#A10A28', '#C7B42C', '#AAAAAA']}"
[results]="filterableData.exercisesProgress"
[gradient]="false"
[xAxis]="true"
[yAxis]="true"
[legend]="true"
[showXAxisLabel]="true"
[showYAxisLabel]="true"
[xAxisLabel]="Date"
[yAxisLabel]="Weight"
[autoScale]="true"
(select)="onSelect($event)">
</ngx-charts-line-chart>
</div>
どのように私は時に値Iがthis.filterableDataに割り当てることができることを確認することができますコンポーネント自体に未定義ではありませんか?テンプレートでは* ngIfを使用できますが、コンポーネントレベルでどのように行うことができますか?また、dropDownSelectedValueは、* ngIfチェックインテンプレートのために明らかに割り当てられていません。
子コンポーネントで最初にapiリクエストを実行しませんか?私は2つの子コンポーネントを持っており、主な理由は2つのAPIリクエストを避けるためです。 –
ああ。とにかく2つの要求をすると思います。次に、このget()をサービスクラスのメソッドに入れ、その結果をサービスのクラス属性に割り当てます。このメソッドを呼び出すときに、値があるかどうかを確認します。はいの場合はそれを返し、そうでない場合は電話をかけます。 – Zolcsi
ermはget要求で観測可能で、それから私は親コンポーネントでそれを購読します。私は子コンポーネントにデータを簡単に渡すことができます。それは1つのリクエストを行い、2つの子コンポーネントに渡された両方の@inputのデータを持っています。唯一の問題は、子コンポーネントの追加配列に再割り当てする方法を知らないことです。私は親コンポーネントで追加の配列を割り当てない限り、私はそれが良いとは思わない、と私はこのシナリオで子コンポーネントで操作する方法を学びたい。 –