【问题标题】:JSON storing HTMLJSON 存储 HTML
【发布时间】:2014-09-11 12:37:31
【问题描述】:

我正在学习 AngularJS 并退出以 JSON 格式存储数据。 我正在构建一个真实的应用程序来学习一个真实的项目。

这是一本在线杂志。现在我将所有文章的基础数据存储在articles.json 中。这样我就可以将所有基础文章数据推送到首页。

[
    {
        "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"
        ]
    }, ...
]

然后根据文章 ID,我将用户引导到一个新模板并加载与 URL 文章 JSON 文件相关的:article1.json。看起来像这样。

 {
    "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"
    ]
}

当然我遇到了问题,在 JSON 中我不能那么容易地使用 HTML 标记,即使可以,将文章段落和标题转换为 JSON 格式也将是一场噩梦。

请指导我采用最佳做法。我假设我在尝试处理数据的方式中遗漏了一些关键部分。也许我应该使用一些数据库来存储数据,而不是 JSON。

请建议使用基于 JS 的框架(如 AngularJS 和数据存储)的最佳实践。

【问题讨论】:

  • 你能提供一个plnkr吗?

标签: json angularjs data-storage


【解决方案1】:

你可以直接在AngularJS中绑定html:

<div ng-repeat="article in articles">
    <div ng-bind-html="article.html_desc"></div>
</div>

【讨论】:

【解决方案2】:

这是一个如何将 HTML 数据添加到 JSON 中的示例。 JSON 可以帮助您非常快速地检索数据并且非常有效。

{
    "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"
}

通过这种方式,您可以存储 html 数据并可以使用密钥访问它。您可以将大量 JSON 对象存储到数组中。

【讨论】:

    猜你喜欢
    • 2022-10-15
    • 2020-10-01
    • 1970-01-01
    • 2019-03-11
    • 2015-06-24
    • 2014-04-19
    • 1970-01-01
    • 2016-09-25
    • 2018-06-10
    相关资源
    最近更新 更多