如何使用mongodb和angularjs在对象数组中打印数据

时间:2018-02-22 21:06:15

标签: angularjs mongodb express mongodb-query

我正在尝试使用ng-repeat将一些数据附加到div。

enter image description here

我使用的是angularjs和没有猫鼬的MongoDB。任何帮助将不胜感激。

来自服务器的对象数组:

[
    {
        _id: "5a733e96aa3c4e1202b347e5",
        image: "lerem",
        title: "Hydration",
        about: "Hydration is key",
        article: "hydor lorem ipsum",
        __v: 0
    },
    {
        _id: "5a7345b1aa3c4e1202b347e6",
        image: "lerem",
        title: "Hydration",
        about: "Hydration is key",
        article: "hydor loremm",
        __v: 0
    },
    {
        _id: "5a735c752aaeb91470fcda61",
        image: "lerem",
        title: "Hydration",
        about: "Hydration is key",
        article: "hydor lorm",
        __v: 0
    }
]

Angularjs ng-repeat:

<div ng-controller="ArticleController" ng-repeat="articles in articles | filter:article" class="articles">
    <h1>{{article.image}}</h1>
    <h1>{{article.title}}</h1>
    <h1>{{article.about}}</h1>
    <h1>{{article.article}}</h1>
</div>

文章控制器:

$http.get("/articles", function(articles) {}

1 个答案:

答案 0 :(得分:0)

<div ng-controller="ArticleController" ng-repeat="article in articles | filter:article" class="articles">
    <h1>{{article.image}}</h1>
    <h1>{{article.title}}</h1>
    <h1>{{article.about}}</h1>
    <h1>{{article.article}}</h1>
</div>

应该有效。请注意,为了区分目的,我将articles in articles更改为article in articles。此外,建议在使用角度时使用Mongoose。有点让事情变得更容易。