简体   繁体   English

无法使用angularjs从json文件获取数据

[英]Data is not get from json file using angularjs

Here is my HTML file and JS file I'm not getting any data's from the JSON file to the html file Below I have HTML files and JS files: 这是我的HTML文件和JS文件,我没有从JSON文件到html文件的任何数据。下面有HTML文件和JS文件:

<!Doctype html>
   <html ng-app>
   <head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
    <script type="text/javascript">
      alert("c");
function imagecontroller($http,$scope){
    alert("cd");
    $http.get('ytdata.json').success(function(ytdata) {
        alert("cdscdsc");
                $scope.data = ytdata;
    });
}  
   </script>
 <style type="text/css">
        li{list-style:none;float:left;padding:20px;}
     a{text-decoration:none;}
 </style>
</head>
          <body>
   <div ng-controller="imagecontroller">
     <ul> 
     <li ng-repeat="datum in data">
              <a>
             <img ng-src="{{datum.thumbUrl}}"/>
                    <br />Id : {{datum.id}} 
                        <br />Purchase : {{datum.Purchase}} 
        </a>
    </li>
</ul>
</div>
   </body>
   </html>

This is my json file: 这是我的json文件:

[
        {
            "id":"mobile.jpg",
            "thumbUrl":"image url",
            "Purchase":20000
        },
        {
            "id":"pendrive.jpg",
            "thumbUrl":"image url",
            "Purchase":400
        },
        {
            "id":"laptop.jpg",
            "thumbUrl":"image url",
            "Purchase":3833
        }

]

Please get me the output for this program 请给我这个程序的输出

Thanks in advance 提前致谢

Here is a working plunker of what you are trying to do : 这是您正在尝试做的工作总结

Controller 调节器

app.controller('MainCtrl', function($scope, $http) {
  $scope.data = null;
  $http.get("data.json").success(function (data) {
    $scope.data = data;
  });
});

View 视图

<body ng-controller="MainCtrl">
  <ul>
    <li ng-repeat="datum in data">
      <a>
        <img ng-src="{{datum.thumbUrl}}" />
        <br />Id : {{datum.id}}
        <br />Purchase : {{datum.Purchase}}
      </a>
    </li>
  </ul>
</body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM