3

Want to build a very simple app that both has some html pages but will allow for an api as the output is just some calculations done by js.

I want to render a json output from my controller and not render html file. Any ideas how to do so with angularjs?

5
  • 1
    You can't. I'm afraid you'll have to use a server-side language for that. Commented Sep 8, 2014 at 7:11
  • You do need a container to output json. Where are you planning to show it, like in a textarea or something like that? Commented Sep 8, 2014 at 7:19
  • I'd like it to return in this format: en.gravatar.com/jack.json Commented Sep 8, 2014 at 7:19
  • You can use JSON.stringify to output json from your controller. But still not sure where you will show it? Commented Sep 8, 2014 at 7:21
  • 1
    I created to plunker for us to have a better understanding. Can you please have a look? I believe it will help me to have a better understanding of your requirement. plnkr.co/edit/CWJbltSNPB0YYPnbnaig?p=preview Commented Sep 8, 2014 at 7:37

2 Answers 2

2

The problem is that a JSON document will be a single request, where the returned content has content type application/json.

If you want Angular.js, which is a frontend library, you need to make a request for an HTML document (and render the necessary HTML markup in the process), then for the angular.js library. What is rendered afterwards is still HTML (content type: text/html).

So the answer is: no, you cannot do that with Angular.js.

You will need a server-side script to output a properly formatted JSON (and nothing else) and the correct Content-Type header.

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

Comments

0

Here is a AngularJS module called json. Use it as a filter in your output.

Usage (In HTML Template Binding):

{{ json_expression | json }}

Usage (In JavaScript):

$filter('json')(object)

Where the object is

Any JavaScript object (including arrays and primitive types) to filter.

Example:

<pre>{{ {'name':'value'} | json }}</pre>

Output:

{
    "name": "value"
}

2 Comments

So there is no way to just render or return just json and not the html. Your solution is still very much using html
You can use this filter without HTML. I have updated my answer. Hope it helps.

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.