如何在angularjs和html中打印json数组对象

时间:2022-12-07 21:42:45

angularjs controller

angularjs控制器

controller('FaqCtrl', function($scope, $http)
{
$http.get("api/index.php/faq/getqusans")
.success(function (response) {
$scope.name = response.catname;

$scope.records = response.catname;

//$scope.question = response.records;

}); 
}).

HTML Code

HTML代码

<div ng-controller="FaqCtrl" class="row">
<div class="" ng-repeat="y in name">
    <h3>{{ y.name }}</h3><br/>
    <div class="col-md-6" ng-repeat="x in records">

        <accordion close-others="true" >
            <accordion-group is-open="status1.open" ng-class="{'expanded': status1.open}" >
                <accordion-heading>{{ x.question }}</accordion-heading>
                {{ x.answer }}
            </accordion-group>

        </accordion>

    </div>
</div>

Json response

Json响应

{
              "catname": [
                {
                  "name": "How do i 2",
                  "records": [
                    {
                      "question": "propose a new dish",
                      "answer": "Anim "
                    }
                  ]
                },
                {
                  "name": "How do i 3h",
                  "records": [
                    {
                      "question": "supply the dishes being demanded by heartyy",
                      "answer": "Anim"
                    },
                    {
                      "question": "sdsffd",
                      "answer": "vcvcvcvcvcv"
                    }
                  ]
                }
              ]
            }

I am not gat the question and answer print in html. only print the name is like 'How do i 2' and 'How do i 2' but name have the quection 'propose a new dish' and answer 'Anim'.

我不懂用html打印的问题和答案。只有打印出来的名字是“How do i 2”和“How do i 2”,但是名字的意思是“提议做一道新菜”和“Anim”。

3 个解决方案

#1


2  

We need to write html as below

我们需要像下面这样写html

JSFIDDLE

JSFIDDLE

HTML CODE

HTML代码

  <div ng-app ng-controller="LoginController">
 <div class="col-md-6" ng-repeat="x in parsedData">
<accordion close-others="true">
    <accordion-group is-open="status1.open" ng-class="{'expanded': status1.open}">
        <div>{{ x.name }}</div>
        <div ng-repeat="y in x.records">
            <accordion-heading><p>question is{{ y.question }}</p></accordion-heading>

JS CODE

JS代码

function LoginController($scope) {
    var dataArr =[];
    var dataJson = {};
    $scope.data = {
              "catname": [
                {
                  "name": "How do i 2",
                  "records": [
                    {
                      "question": "propose a new dish",
                      "answer": "Anim "
                    },
                      {
                      "question": "propose a new dish",
                      "answer": "Anim "
                    },
                      {
                      "question": "propose a new dish",
                      "answer": "Anim "
                    }
                  ]
                },
                {
                  "name": "How do i 3h",
                  "records": [
                    {
                      "question": "supply the dishes being demanded by heartyy",
                      "answer": "Anim"
                    },
                    {
                      "question": "sdsffd",
                      "answer": "vcvcvcvcvcv"
                    }
                  ]
                }
              ]
            };

    $scope.parsedData = $scope.data.catname;
    console.log($scope.parsedData);
};

o/p:

o / p:

How do i 2
question ispropose a new dish

answer is Anim

question ispropose a new dish

answer is Anim

question ispropose a new dish

answer is Anim

How do i 3h
question issupply the dishes being demanded by heartyy

answer is Anim

question issdsffd

answer is vcvcvcvcvcv

JSFIDDLE

JSFIDDLE

#2


1  

privi_d1: JSON[];
  json: JSON[];

ngOnInit() {
    this.http.get('http://localhost/feitoria/php/device.php')
      .subscribe((data) => {
        this.json = JSON.parse(data.text()).device;
        this.privi_d1 = this.json[0][3];
      }, (error) => {
        console.log(error);
      });
  }

#3


0  

Try this:

试试这个:

<div class="col-md-6" ng-repeat="x in catname">
    <accordion close-others="true">
        <accordion-group is-open="status1.open" ng-class="{'expanded': status1.open}">
            <accordion-heading>{{ x.records.question }}</accordion-heading>
            {{ x.records.answer }}
        </accordion-group>
    </accordion>  
</div>

#1


2  

We need to write html as below

我们需要像下面这样写html

JSFIDDLE

JSFIDDLE

HTML CODE

HTML代码

  <div ng-app ng-controller="LoginController">
 <div class="col-md-6" ng-repeat="x in parsedData">
<accordion close-others="true">
    <accordion-group is-open="status1.open" ng-class="{'expanded': status1.open}">
        <div>{{ x.name }}</div>
        <div ng-repeat="y in x.records">
            <accordion-heading><p>question is{{ y.question }}</p></accordion-heading>

JS CODE

JS代码

function LoginController($scope) {
    var dataArr =[];
    var dataJson = {};
    $scope.data = {
              "catname": [
                {
                  "name": "How do i 2",
                  "records": [
                    {
                      "question": "propose a new dish",
                      "answer": "Anim "
                    },
                      {
                      "question": "propose a new dish",
                      "answer": "Anim "
                    },
                      {
                      "question": "propose a new dish",
                      "answer": "Anim "
                    }
                  ]
                },
                {
                  "name": "How do i 3h",
                  "records": [
                    {
                      "question": "supply the dishes being demanded by heartyy",
                      "answer": "Anim"
                    },
                    {
                      "question": "sdsffd",
                      "answer": "vcvcvcvcvcv"
                    }
                  ]
                }
              ]
            };

    $scope.parsedData = $scope.data.catname;
    console.log($scope.parsedData);
};

o/p:

o / p:

How do i 2
question ispropose a new dish

answer is Anim

question ispropose a new dish

answer is Anim

question ispropose a new dish

answer is Anim

How do i 3h
question issupply the dishes being demanded by heartyy

answer is Anim

question issdsffd

answer is vcvcvcvcvcv

JSFIDDLE

JSFIDDLE

#2


1  

privi_d1: JSON[];
  json: JSON[];

ngOnInit() {
    this.http.get('http://localhost/feitoria/php/device.php')
      .subscribe((data) => {
        this.json = JSON.parse(data.text()).device;
        this.privi_d1 = this.json[0][3];
      }, (error) => {
        console.log(error);
      });
  }

#3


0  

Try this:

试试这个:

<div class="col-md-6" ng-repeat="x in catname">
    <accordion close-others="true">
        <accordion-group is-open="status1.open" ng-class="{'expanded': status1.open}">
            <accordion-heading>{{ x.records.question }}</accordion-heading>
            {{ x.records.answer }}
        </accordion-group>
    </accordion>  
</div>