【问题标题】:how to print a list from json document如何从 json 文档中打印列表
【发布时间】:2019-02-16 22:49:47
【问题描述】:

我想从 list.json 文件中打印我的数据

    xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET","list.json",false);
    xmlhttp.send();
    var list = xmlhttp.responseText;

这是我的 list.Json

{

"p1": {
    "name":"fruit song",
    "cat":"learning",
    "lan":"eng",
    "ur":"abc1"
},
"p2": {
    "name":"eting sugar",
    "cat":"random",
    "lan":"eng",
    "ur":"abc2"
},
"p3": {
    "name":"salgira ka din",
    "cat":"birthday",
    "lan":"urdu",
    "ur":"abc3"
},
"p4": {
    "name":"twinkle twinkle",
    "cat":"random",
    "lan":"eng",
    "ur":"abc4"
},
"p5": {
    "name":"abc song",
    "cat":"learning",
    "lan":"eng",
    "ur":"abc5"
}

}

我正在获取类似

的数据
obj = JSON.parse(list);
$scope.n =obj.p1.name;
$scope.c =obj.p1.cat;
$scope.l =obj.p1.lan;
$scope.u =obj.p1.ur;

这工作正常并在 alert($scope.n) 中显示数据;

我想用类似卡片的 html 格式打印这个 json 文件

p1 名称:{{n}} 类别:{{c}} 语言:{{l}}

p2 名称:{{n}} 类别:{{c}} 语言:{{l}}

list.json 里面会有 50+ P1 类型的记录。

谁能告诉我用html打印所有记录的代码

【问题讨论】:

    标签: javascript html json ionic-framework angularjs-ng-repeat


    【解决方案1】:

    所以我的html是

    <li ng-repeat="item in items"   item="item" ng-click="model.itemValue = item" >
    <div class = "row" style="background: url(img/4.jpg) no-repeat center;background- 
    size:cover;">
    <div class = "col"><h3>{{name}} :)</h3></div>
    <div class = "col col-60"><h3>{{email}}</h3></div>
    <div class = "col"><h3><button class="button button-calm button rdm">PLAY</button> 
    </h3></div>
    </div>
    </li>
    

    我的控制器是

     xmlhttp=new XMLHttpRequest();
     xmlhttp.open("GET","Ulist.json",false);
     xmlhttp.send();
     var list = xmlhttp.responseText;
     obj = JSON.parse(list);
    
     $scope.data = {};
    
     $scope.model = {};
    
     $scope.items = obj;
    

    这对我有用

    【讨论】:

      【解决方案2】:

      你应该使用 AngularJS 的 ng-repeat,比如

      <div>
        <p ng-repeat="(key, val) in obj">{{key}} name: {{val.name}} category : {{val.cat }} language : {{val.lan}}</p>
      </div>
      

      为了将来的参考,请使用 AngularJS 文档:ngRepeat

      【讨论】:

      • 这个 ng-repeat 对我有用.. 谢谢你的帮助/
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-12
      • 1970-01-01
      • 2019-08-16
      • 1970-01-01
      • 2014-02-23
      相关资源
      最近更新 更多