0

Hello i am new to angular and i can not figure it out to show html response data in component below is my code please someone help with this. Thanks

data.Service.ts (here i call an API service which return some html)

getLibrary() {return this.http.get('http://localhost:8000/api/user/library');}

Library.component.ts

import { Component, OnInit } from '@angular/core';
import { DataService } from '../data.service';
import { Observable } from 'rxjs';

@Component({
  selector: 'app-library',
  templateUrl: './library.component.html',
  styleUrls: ['./library.component.scss']
})
export class LibraryComponent implements OnInit {

  library: any;

  constructor(private data: DataService) { }

  ngOnInit() {
    this.data.getLibrary().subscribe(
      data => this.library = data 

    );



  }

}

Library.component.html

{{library}}

Error

core.js:14576 ERROR 
HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "http://localhost:8000/api/user/library", ok: false, …}
error: {error: SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttp…, text: "<div style="display:block" class="libContainer lib…            
↵           </div>
↵
↵
↵</div>
↵
↵
↵
↵
↵
↵
↵
↵
↵"}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure during parsing for http://localhost:8000/api/user/library"
name: "HttpErrorResponse"
ok: false
status: 200
statusText: "OK"
url: "http://localhost:8000/api/user/library"
__proto__: HttpResponseBase
3
  • what does the response look like? please add to code. not looking like a JSON response Commented Dec 17, 2018 at 11:10
  • response is HTML only no JSON Commented Dec 17, 2018 at 11:14
  • response is like => <h1>Some Html Content</h1> Commented Dec 17, 2018 at 11:17

1 Answer 1

2

returned datatype is text you can use like this

 ngOnInit() {
        this.data.getLibrary().subscribe(
          (data:(any)) => this.library = data 

        );

in view

<div [innerHTML]="library"></div>
Sign up to request clarification or add additional context in comments.

3 Comments

yes now html is rendered on view after using json_encode(); but not rendering as HTMl its rendering as text on view
in view use <div [innerHTML]="library"></div>
Thank u so much its working now..have a great day :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.