当列出3时,ANGULARJS结果消失

时间:2017-02-14 05:24:39

标签: php html mysql angularjs angularjs-ng-repeat

我的查询搜索餐厅在其数据库上的所有菜肴。

<?php
include('base.php');
$data = array();
$result = mysql_query("SELECT nombre FROM platos WHERE tipo = 'principal'",$connect);
if(mysql_num_rows($result) > 0){
    while($row = mysql_fetch_assoc($result)){
        $data[] = $row;
    }
} else {
    echo "0 results";
};
echo json_encode($data);
mysql_close($connect);
?>

然后我在角度代码上调用它:

$http({
      method: 'GET',
      url: '../../php/getDishesPrincipal.php'
    }).then(function successCallback(response) {
        $scope.principals = response.data;
    }, function errorCallback(response) {
        $scope.principals = 'No Response';
    });

好吧,最后我把它打印在我的HTML代码上:

<div id="agregarpedido_table_item" ng-repeat="principal in principals">
   <p id="agregarpedido_table_item_p">{{principal.nombre}}</p>
   <div id="agregarpedido_table_item_command">
     <p>{{principal.cant}}</p>
     <div class="selectors">
       <input type="button" value="+" ng-click="principal.cant = principal.cant + 1">
       <input type="button" value="-" ng-click="principal.cant = principal.cant - 1">
     </div>
   </div>

问题在于,当我的数据库中有超过2个匹配tipo:principal的结果时,html消失了,它没有显示任何内容。最糟糕的是,控制台不会抛出任何错误,就像它是正常的一样。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

因为你的SQL查询只返回一个名字数组,所以angular可能会在ng-repeat中抛出一个无重复的错误。

尝试格式化ng-repeat,如下所示:ng-repeat="principal in principals track by $index"

您的主体可能有2个或更多非唯一名称,因此角度抛出无重复错误。作为参考,使用track by $ index方法可以帮助改善循环中的渲染/绑定性能。

这是一个jsbin https://jsbin.com/noxawusoqe/2/edit?html,js,console,output

希望有效

答案 1 :(得分:0)

我从未找到答案,但当我重新启动电脑时,错误就消失了。