html - Change response type of a web service to JSON from XML so that I can retrieve data - angular -
the data want display listed @ web page xml , looks this:
how can retrieve xml data in situation?
the way have tried implement service , model each project, shown here:
my code:
project.model.ts:
export class project { id: number; name: string; status: boolean; constructor(obj: any) { this.id = parseint(obj.id); this.name = obj.name; this.status = obj.status; } }
project.service.ts:
export abstract class projectservice { abstract fetchprojects(): observable<project[]>; }
project.service.http.ts:
@injectable() export class projectservicehttp extends projectservice { baseurl = "http://dev-teamcity:8090/app/rest/projects/"; constructor(private http: http) { super(); } fetchprojects(): observable<project[]> { return this.http .get(this.baseurl) .map(resp => resp.json()) .map(jsonarray => jsonarray.map((item: string) => new project(item))); } }
project.viewer.component.ts:
@component({ selector: 'project-viewer', templateurl: 'app/components/project-viewer.html', styleurls: ['app/project-viewer.css'] }) export class projectviewercomponent { name = 'projectviewercomponent'; projects: project[]; errormessage = ""; statevalid = true; constructor(private service: projectservice) { this.fetchprojects(); } private fetchprojects() { this.service .fetchprojects() .subscribe(projects => this.projects = projects, () => this.raiseerror("no projects found!")); } private raiseerror(text: string): void { this.statevalid = false; this.errormessage = text; } }
project-viewer.html
<h3>projects </h3> <div > <ul class= "grid grid-pad"> <a *ngfor="let project of projects" class="col-1-4"> <li class ="module project"> <h4 tabindex ="0">{{ project.id }}</h4> </li> </a> </ul> </div>
Comments
Post a Comment