【发布时间】:2017-01-16 16:31:30
【问题描述】:
我正在尝试在我存储了几个数组的本地服务器上获取一个 json 文件。我试图通过使用代码运行如下的 $http 服务来实现这一点:
(function(){
var app = angular.module('store', ['store-products']);
app.controller('StoreController',['$http', function($http){
var store = this;
store.products = [];
$http.get('products.json').success(function(data){
store.products = data;
})
})();
还有我的 products.json 文件:
[ {
"name" : "Lasagna",
"price" : 250,
"images":[{"full":"images/lasagne.jpg"}],
"Description":"Lasagne are wide, flat-shaped pasta, and possibly one of the oldest types of pasta.The word 'lasagne', and, in many non-Italian languages, the singular 'lasagna', can also refer to a dish made with several layers of lasagne sheets alternated with sauces and various other ingredients.",
"canPurchase": true,
"Reviews":[]
},
{
"name":"Shawarma",
"price":300,
"images":[{"full":"images/shawarma.jpg"}],
"Description":"Shawarma or Shawurma is a Levantine meat preparation, where lamb, chicken, turkey, beef, veal, buffalo meat, or mixed meats are placed on a spit (commonly a vertical spit in restaurants), and may be grilled for as long as a day.Shavings are cut off the block of meat for serving, and the remainder of the block of meat is kept heated on the rotating spit. Shawarma can be served on a plate (generally with accompaniments), or as a sandwich or wrap. Shawarma is usually eaten with tabbouleh, fattoush, taboon bread, tomato, and cucumber. Toppings include tahini, hummus, pickled turnips, and amba",
"canPurchase": true,
"Reviews":[]
}]
还有我的html代码:
<!doctype html>
<html ng-app="store">
<head>
<link rel="stylesheet" href="styles/bootstrap.min.css">
</head>
<body ng-controller="StoreController as store">
<h1 align="center"><ul>Food Junkie</ul></h1>
<h2 align="center">-It's all gonna be alright-</h2>
<ul class="list-group">
<li class="list-group-item" ng-repeat ="product in store.products">
<product-title></product-title>
<product-images></product-images>
<button ng-show="product.canPurchase"><img src="images/cart_64x64.jpg"/></button></br>
<product-panel></product-panel>
</li>
</ul>
<script type="text/javascript" src="scripts/angular.min.js"></script>
<script type="text/javascript" src="scripts/ang.js"></script>
</body>
</html>
【问题讨论】:
-
当您直接从网络浏览器或REST客户端请求
<server_url>/product.json时,您会得到什么结果? -
@AsielLealCeldeiro:我得到了我列出的数组。
标签: javascript angularjs arrays json