0

I am learning AngularJS and exited about storing data in JSON format. I am building a real app to learn on a real project.

It's an online magazine. Now I store all the articles basic data in articles.json. This way I can push all the basic articles data to the home page.

[
    {
        "id":"1",
        "category": "Activity",
        "title": "Title goes here",
        "short_desc": "Short description goes here",
        "images": [
            "img/article-img.jpg"
        ]
    },
    {
        "id":"1",
        "category": "Activity",
        "title": "Title goes here",
        "short_desc": "Short description goes here",
        "images": [
            "img/article-img.jpg"
        ]
    }, ...
]

Then based on the article ID I direct user to a new template and load relevant to URL article JSON file : article1.json. It looks like this.

 {
    "id":"1",
    "category": "Activity",
    "title": "Title goes here",
    "html_desc": "<h1>Article subtitle goes here<h1><p>Paragraph text<\p>",
    "images": [
        "img/featured-img.jpg"
    ]
}

And of course I run into problem, that in JSON I can't that easily use HTML tags or even if I could, it will be a nightmare to convert article paragraphs and headings to JSON format.

Please direct me to the best practice. I assume I am missing some crucial parts in the way I am trying to handle data. Maybe I should use some database for storing data, not JSON.

Please advice the best practice of using JS based frameworks like AngularJS and data storage.

1
  • can you provide a plnkr? Commented Sep 11, 2014 at 12:47

2 Answers 2

3

you can bind html directly in AngularJS:

<div ng-repeat="article in articles">
    <div ng-bind-html="article.html_desc"></div>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

Yes that worked. I added ngSanitize to the app as described here: docs.angularjs.org/api/ng/directive/ngBindHtml. Also note to everyone who will be using HTML in JSON, to avoid multiline problem, use HTML minfication services like willpeavy.com/minifier to convert your nice HTML to one line.
1

This is an example of how to add HTML data into JSON. JSON helps you to retrieve data very fast and is very effective.

{
    "title":"mongoose connect",
    "description":"<p> Aaron, thanks. I read the doc on connection pools and think I understand it. The problem I am having is defining the db connection in the app.js file and then using that connection in my model definition files (one model per file) and in the routing files. I've tried the global.conn idea that Daniel providedcouldn't get it to work and the separate index.js file that Sergey suggested but couldn't get it work either. Is there an example of an express appsp that uses createConnection to create one connection that is shared in separate modules? – </p>",
    "tags":["node js","mongoose","database mongodb connect","mongoose connect"],
    "code":"<code> var mongoose = require('mongoose');<br> &nbsp;mongoose.connect('mongodb://127.0.0.1:27017/mytest'); </code>",
    "created_on":" -- 04/10/2015",
    "author":" -- by aqib"
}

This way you are able to store html data and can access it using a key. You can store numerous JSON objects into array.

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.