角度2のモーダルをポップアップして人のリストを表示しようとしています。リストのソースはJSONファイルです。私は、データがモーダルのテーブルに正しくバインドされていないと思います。私はアンジェルス2の新人ですが、私が何が欠けているのか分かりません。 戻り-JSONアレイ-service.ts角度2のモーダルにJSONデータを渡す方法
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs';
@Injectable()
export class ReturnsJsonArrayService {
constructor(private http: Http) {}
getPeople(): Observable<any> {
return this.http.request('./people.json')
.do(res => console.log('HTTP response:', res))
.map(res => res.json().payload)
.do(console.log);
//.map(res => res.json());
/*return this.http.get('./people.json')
.map((res:Response) => res.json())
.catch((error:any) => Observable.throw(error.json().error || 'Server error'));*/
}
}
サンプルJSONファイル:
{
"id": "1",
"name": "David Martinez Ros",
"email": "[email protected]",
"age": "33"
},
{
"id": "2",
"name": "Paco Roberto Corto",
"email": "[email protected]",
"age": "51"
},
{
"id": "3",
"name": "Silvia Elegante i Latina",
"email": "[email protected]",
"age": "30"
}
]
モーダルcomponent.ts
import {Component, Input} from '@angular/core';
import {NgbModal, NgbActiveModal} from '@ng-bootstrap/ng-bootstrap';
import { Observable } from 'rxjs';
import { ReturnsJsonArrayService } from './returns-json-array.service';
@Component({
selector: 'ngbd-modal-content',
providers: [ReturnsJsonArrayService],
template: `
<div class="modal-header">
<h4 class="modal-title">Hi there!</h4>
<button type="button" class="close" aria-label="Close" (click)="activeModal.dismiss('Cross click')">
<span aria-hidden="true">×</span>
</button>
<div class="modal-body" *ngFor="let person of peopleData | async" >
<p>One fine body…</p>
<table border=1>
<tr>
<td>
<h3>Id: {{ person.id }}</h3>
</td>
<td>
<h3>name: {{ person.name }}</h3>
</td>
<td>
<h3>email: {{ person.email }}</h3>
</td>
<td>
<h3>age: {{ person.age }}</h3>
</td>
<td>
</tr>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" (click)="activeModal.close('Close click')">Submit</button>
</div>
`
})
export class NgbdModalContent {
@Input() name;
@Input() peopleData: Observable<Array<any>>;
constructor(public activeModal: NgbActiveModal,private peopleService: ReturnsJsonArrayService) {
this.peopleData = this.peopleService.getPeople();
console.log("AppComponent.data:" + this.peopleData);
}
}
@Component({
selector: 'ngbd-modal-component',
templateUrl: './modal-component.html'
})
export class NgbdModalComponent {
constructor(private modalService: NgbModal) {}
open() {
const modalRef = this.modalService.open(NgbdModalContent);
modalRef.componentInstance.name = 'Barb' ;
console.log("Peopledatra on open():" + modalRef.componentInstance.peopleData);
}
}
people.json JSONファイルを読み込むための
サービス
modal-component.html
<button class="btn btn-primary" (click)="open()">Assign</button>
問題はなんですか?現在の行動とは何か、期待される行動は何ですか? ***私はデータが適切にモーダル***のテーブルにバインドされていないと思う***多くは私達に教えてくれない;) – Alex