2017-12-23 14 views
0

다음 코드를 사용하여 JSON 파일에서 데이터를로드하고 ion-listion-item으로 표시하지만 작동하지 않습니다. 내가하는 일에 무엇이 잘못되었는지를 알 수는 없다.ng-repeat로 ion-item에 JSON 파일의 내용 표시

import { Component } from "@angular/core"; 
import { HttpClient} from "@angular/common/http"; 

/** 
* Interface for a single Call entry 
* A Call always has a 'name:string' and a 'telephone:string', any 
* other attribute is optional. 
*/ 
export interface Call { 
    name:   string, 
    telephone:  string, 
    description?: string, 
    street?:  string, 
    postal?:  string 
} 

/** 
* Interface for a list of Call entries 
*/ 
export interface CallList { 
    calls: Call[]; 
} 

/** 
* Class for a page that shows Call entries 
*/ 
@Component({ 
    selector: 'page-calls', 
    templateUrl: 'calls.html' 
}) 
export class CallsPage { 

    callList: CallList; 

    /** 
    * Constructor of CallsPage 
    * @param http 
    */ 
    constructor(public http: HttpClient) { 
     this.loadCalls('assets/json/calls.json'); 
     // console.log(this.callList); // does not work, just gives "undefined" 
    }; 

    /** 
    * Loads list of calls from a json file 
    * @param filePath: Path to file that shall be loaded 
    */ 
    loadCalls(filePath: string) { 
     return this.http.get<CallList>(filePath).subscribe(
      data => { 
       this.callList = data; 
       // console.log(data); // works 
      } 
     ); 
    }; 
} 

calls.json 이것은

<ion-header> 
    <ion-navbar> 
     <button ion-button menuToggle> 
      <ion-icon name="menu"></ion-icon> 
     </button> 

     <ion-title>Calls</ion-title> 
    </ion-navbar> 
</ion-header> 

<ion-content> 
    <ion-list> 
     <ion-item ng-repeat="call in callList"> 
      {{call.name}}: {{call.telephone}} 
     </ion-item> 
    </ion-list> 
</ion-content> 

을 사용하는 템플릿 calls.html입니다

[ 
{"name": "Police","telephone": "110","description": ""}, 
{"name": "Fire","telephone": "112","description": ""} 
] 

포함하지만 내 응용 프로그램에서이 페이지를 열 때, 다음과 같은 콘솔에 나타납니다 Unhandled Promise rejection: _co.call is undefined ; Zone: <root> ; Task: Promise.then ; Value: . JSON에서 데이터를 가져 오는 중 여전히 잘못된 방법이 있다고 생각합니다. 주석 처리 된 생성자의 행은 콘솔에 undefined을 제공합니다. 그러나 .subscribe 호출에서 같은 작업을 수행하면 데이터가 인쇄됩니다. 그래서 나는 문제가 어디에 있는지 알 수 없다.

+0

입니다 ng-repeat으로 사용할 필요가, 당신의 오류는 일부를 가리 키도록 보인다 우리가 당신의 코드에서 볼 수없는'emergencyCall' ... – Alex

답변

3

당신은 angular 구문 ngFor 대신에 설립 것처럼`ngFor`해야 그 외에 angularjs 1.x 구문

<ion-item *ngFor="let call of callList"> 
    {{call.name}}: {{call.telephone}} 
</ion-item> 
+0

고마워요, 이것 역시 잘못되었습니다. 이제' {{n}}'중 n 개가 작동하도록합니다. 그러나 실제로 표시하고자하는 데이터는 여전히 '정의되지 않은'것으로 보이므로 작동하지 않습니다. 그러나 이제는 하나의 문제가 해결되었습니다. – herhuf