使用Angular Js ng-repeat

时间:2015-10-01 15:33:26

标签: javascript json angularjs cordova angularjs-controller

我能够从php文件中检索以下JSON数据(测试数据):

JSON:

[{"id":"1","name":"shabri","desc":"bbkbjkbjbjnnklnln","location":"location","mobile":"498534534","telephone":"4549385","offer":"20","email":"nfnkjrnfnrndnrgnkjr"},{"id":"2","name":"bhagatfergdfgfdg","desc":"vfdgfdbgbbgbg","location":"fbgbgfbgfb","mobile":"544656757","telephone":"4223424","offer":"30","email":"vdsxdvgvgv"},{"id":"3","name":"rddfdgdf","desc":"bdffgd","location":"fghgfhfhgf","mobile":"8598","telephone":"856845","offer":"6","email":"httrdh6kiki"}] 

Angular Js控制器

.controller('RestaurantsCtrl',  ['$scope', '$http', function ($scope, $http) {
$http.get('http://xyz/getRestros.php')


.success(function(data) {
  alert(data);
  $scope.Restaurants = data;
  });
}])


我确信检索到JSON数据是因为为了测试我有警报(数据),它在警告框中显示正确的json编码数据。


问题是数据未被分配到$ scope.Restaurants。所以html中的ng-repeat没有填充列表。

如果我硬代码$ scope.Restaurants。

,则ng-repeat填充列表

这里有什么错误?请帮忙..

修改

硬编码$ scope。工作的餐馆:


$scope.Restaurants = [
{ name: 'Shabri', id: 1 },
{ name: 'Bhagat Tarachand', id: 2 },
{ name: 'Udipi', id: 3 },
{ name: 'Shahu', id: 4 },
{ name: 'Bagdadi', id: 5 },
{ name: 'Shiv Sagar', id: 6 }

];


NG-重复:

<a class="item item-thumbnail-left" ng-repeat="Restaurant in Restaurants | filter: searchText" href="#/app/Restaurants/{{Restaurant.id}}">
  <img src="img/default_restro.png" style="margin-top:30px;"/>
  <h2 style="font-weight:bold;">{{Restaurant.id}}</h2>
  <p>{{Restaurant.name}}</p><br>
  <span style="white-space: pre-wrap; font-weight:300;">This is the description about the restaurant click to know more.</span><br>


</a>

3 个答案:

答案 0 :(得分:0)

您可以在Chrome开发者工具视图(Ctrl-Shift-I)中进行调试并设置断点,以查看$ scope.Restaurant是否在行后分配了值     $ scope.Restaurants = data;

答案 1 :(得分:0)

您需要将返回的JSON字符串解析为对象。

尝试

arrayData = JSON.parse(data);
$scope.Restaurants = arrayData;

答案 2 :(得分:0)

它实际上并不是一个答案,但我找到了上述问题的根本原因,可能会发生在某人身上。

我用来托管该php文件的免费虚拟主机是罪魁祸首。

每次执行php时,网络托管在对象结束后最后添加了一些谷歌分析脚本。在实时编辑该文件时,该脚本不会出现。我不知道怎么做。

无论如何,当我再次调试$ scope.Restaurants时,我发现了它。

似乎如果其他任何东西与JSON对象一起回应,它会序列化并将其视为字符串。

在更正和更改主机后,发现了差异并将其视为实际的json对象,并且工作正常。