列出 3 个时,ANGULARJS 结果消失



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

<?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 会消失并且不显示任何内容。最糟糕的是,控制台不会抛出任何错误,就好像它是正常的一样。有什么想法吗?

由于SQL查询,您只返回了一个名称数组,因此angular可能会在ng-repeat中抛出无重复错误。

尝试像这样格式化你的ng重复:ng-repeat="principal in principals track by $index"

您的主体可能有 2 个或更多非唯一名称,因此 Angular 会抛出无重复错误。作为参考,使用 Track by $index 方法有助于提高循环中的呈现/绑定性能。

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

希望有效

我从未找到答案,但是当我重新启动PC时,错误单独消失了。

最新更新