【问题标题】:angular js show only the last dataangular js只显示最后的数据
【发布时间】:2017-11-09 14:16:50
【问题描述】:

我有两个表,customers 和 lists,lists 有一个外键,并且必须显示客户拥有的列表。清单很多,客户只有一个。 mysql中的一对多。

$scope.getCustomer = function (id) {

      $http.get("/customers/getone/" + id)

      .then(function successCallback (response, data, status, headers, config) {

          $('#edit').modal('show');

          $scope.nameC = response.data[0].nameC;
          $scope.id=id;
          //$scope.clearForm()

          for (i = 0; i < response.data.length; i++) {

            $scope.nameL = response.data[i].nameL;
            //alert($scope.nameL);

        }



       },

  function errorCallback(error, data, status, haders, config) {
      console.log('Error getting: ' + data);

   });
}

我有所有列表,但在 html 中我只有最后一个列表。我有一个 ng-repeat 向所有客户展示。我想要一个 ng-repeat 或一个 ng-bind 向选定的客户显示一个客户和所有列表

<label>Name Kunde</label>
                 <td>
                 <span ng-bind="nameC"></span>
              </td>
              <br>
              <label>Name Liste</label>
              <td>
              <span ng-bind="nameL"></span>
           </td>



           <table class="table table-bordered">  
            <tr>  
                 <th>Name Listen</th>

            </tr>  

            <tr ng-repeat="data in [nameL] track by $index">  

             <td>{{data}}</td>  


             <td>



               </td>
            </tr>  
       </table> 

【问题讨论】:

    标签: javascript angularjs for-loop


    【解决方案1】:

    要显示所有列表,您需要将所有列表存储在您的作用域中。此时,您只将最后一个列表存储在您的作用域中。

    $scope.lists = [];
    
    for (i = 0; i < response.data.length; i++) {
        $scope.nameL = response.data[i].nameL;
        $scope.lists.push(nameL);
        //alert($scope.nameL);
    }
    

    然后在 HTML 中显示您的列表列表

    <table class="table table-bordered">  
        <tr>  
            <th>Name Listen</th>
        </tr>  
        <tr ng-repeat="data in lists track by $index">  
             <td>{{data}}</td>  
             <td>
             </td>
        </tr>  
    </table>
    

    【讨论】:

    • 非常感谢:这里一定是:$scope.lists.push($scope.nameL); 1分有用的答案:)谢谢
    猜你喜欢
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 2023-01-19
    • 2017-09-23
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    相关资源
    最近更新 更多