【问题标题】:ANGULARJS results disappear when 3 are listed列出 3 个时,ANGULARJS 结果消失
【发布时间】:2017-02-15 15:17:36
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    由于您的 SQL 查询仅返回一组名称,因此 Angular 可能会在 ng-repeat 中引发 no-dupes 错误。

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

    您的主体可能有 2 个或更多非唯一名称,因此 angular 会引发 no dupe 错误。作为参考,通过 $index 方法使用 track 可以帮助提高循环中的渲染/绑定性能。

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

    希望有效

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-26
        • 1970-01-01
        • 2016-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多