0

Is it possible to return JSON result from Angular instead of the HTML template coz we want to build something similar to a API-server? Thanks.

Here is the example that return the HTML template, how can we just return JSON without using template?

What I want to return is just a simple json result instead of HTML.

{"ID" : "1", "Name" : "Apple"} 

Here is the code.

import { Component, OnInit, Input } from '@angular/core';
@Component({
  selector: 'app-noresultsfound',
  templateUrl: './noresultsfound.component.html',
  styleUrls: ['./noresultsfound.component.css']
})
export class NoresultsfoundComponent implements OnInit {

  @Input() txtval: string;

  constructor(private app: AppConstants) { }

  noresultsfound = this.app.noresultsfound;

  ngOnInit() {
  }
}

Alex

7
  • For example..?? Commented Apr 25, 2019 at 5:12
  • Thanks but still it's not clear what you are asking! Do you want to return JSON? like {"key" : "value"} etc or HTML like <div>This is HTML content</div>. Commented Apr 25, 2019 at 5:56
  • and what you mean by return? From function? from API? Commented Apr 25, 2019 at 5:56
  • You need to be clear what you want to return from where (API, function, input etc....) So we can help you Commented Apr 25, 2019 at 6:32
  • Thanks again, I updated as well. I just want to build a mock server to return some simple JSON result. Commented Apr 25, 2019 at 10:33

1 Answer 1

0

I think, he meant that pure JSON should be returned to who ever requested it by endpoint request. At least i faced something like this. For static json, i've found an answer: https://github.com/angular/angular-cli/issues/5029

bresleveloper commented on 6 Jul 2018 1. ng-build with your index.html set properly with its components. (or conditional app-components) 2. rename and copy the rendered to (for example) /src/search.html 3. in angular.json (angular-cli.json for pre v5) find "assets":

"assets": [
"src/favicon.ico",
"src/search.html",
"src/assets"
],

browse localhost:4200/search.html enjoy :)

Interesting part comes when u try to generate that json somehow, with browser not being involved - like some automatic service sends a request to some angular endpoint, like: hosname/statistics and in response, it receives a json which depends on number of pictures and headers on this current hosname, like {siteName: 'test', pictures: 10, headers: 1}.

Sign up to request clarification or add additional context in comments.

Comments

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.