使用angularjs访问json数据

时间:2016-03-04 16:07:38

标签: angularjs json ionic-framework

我发现很难像这样访问json数据。 如果这是我的json数据。如何使用带离子框架的angularjs在html中访问它。

do something

这是我的角色

{"status": "OK",    
 "message": "OK",
"data": {
"total": 116,
"params": [],
"heading": "Psychology Blog, Total 116",
"sub_option": "blog",
"text1": "Read more",
"rows": [
  {
    "post_id": "124",
    "user_id": "1443980348",
    "post_type": "post",
    "post_format": "text",
    "post_name": "is-money-most-important-in-life-",
    "post_title": "Is Money most Important in life?",
"alt_text": "alt=\"\" /></span></p>"
  },
  {
    "post_id": "122",
    "user_id": "1443980348",
    "post_type": "post",
    "post_format": "text",
    "post_name": "should-divorcing-parents-make-their-child-choose-",
    "post_title": "Should divorcing parents make their child choose?",
"alt_text": "alt=\"\" /></p>"
  }
]
"paging": {
  "now": 100,
  "next": 2,
  "page": 1,
  "limit": 100,
  "start": 0,
  "total": 116,
  "limitstart": 0,
  "nowTotal": 100
}
}
}

我尝试过使用ng-repeat但是没有用。请帮忙。

3 个答案:

答案 0 :(得分:0)

更改标题的内容类型

'Content-Type': 'application/x-www-form-urlencoded' 

'Content-Type': 'application/json'

这将解决您的问题。

您可以将数据显示为

<div ng-repeat="r in post.rows">  {{r.post_id}} , {{r.post_title}}</div>

答案 1 :(得分:0)

PostsController

(function () {
    'use strict';

    angular.module('app', [])
        .controller('PostsController', PostsController);

    function PostsController($scope, $http) {

        $scope.posts = [];

        //activating controller
        activate();
        function activate() {
            // using $http directly on Controller is a bad idea;
            // create a Service
            $http.get('your url')
                .then(function (response) {
                    $scope.posts = [].concat($scope.posts, response.data.rows)
                    // according to your data provided
                })
        }
    }
})();

posts.view.html

<div ng-repeat="post in posts">
   <h4>post.title</h4>
   .........

</div>

答案 2 :(得分:0)

地狱大家, 我得到了答案。 我们可以用来访问json数据的方法,如上所述(以多维数组的形式)。

第一种方法:在您的控制器中 添加

var yourVariable = data['data']['rows']; $scope.htmlVariable = yourVariable;

并在你的html中添加

<div ng-repeat="x in htmlVariable"><p>{{x.id}}</p></div>

这可能对其他人有所帮助,我要感谢所有人的回应。