doufang1954 2018-04-27 02:09
浏览 66
已采纳

AngularJS HTTP请求与另一个功能

I have a simple app that reads data from a JSON file and displays it in a webpage. That part works fine (the displaying). I now want to be able to sort through the table by toggling on the table headers using the orderBy filter in AngularJS ... however, that's where I have issues.

What may I be overlooking? I feel like it has to do with the HTTP Function ... could that be messing with it? I don't know why that may be an issue though?

Please find my code attached.

leads.php

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title></title>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
  </head>
  <body>

    <div ng-app = "myApp" ng-controller = "myCtrl">

      <textarea name="name" ng-model = "test"></textarea>

      <h1>{{ count }}</h1>

      <table class="table">
        <thead>
          <tr>
            <th ng-click = "sortBy('first_name')">First Name</th>
            <th ng-click = "sortBy('last_name')">Last Name</th>
            <th ng-click = "sortBy('email')">Email Address</th>
            <th ng-click = "sortBy('accountCreation')">Account Creation</th>

          </tr>
        </thead>
        <tbody>
          <tr ng-repeat = "user in myData | orderBy : orderByThis">
            <td> {{user.first_name}} </td>
            <td> {{user.last_name}} </td>
            <td> {{user.email}} </td>
            <td> {{user.accountCreation}}</td>
          </tr>
        </tbody>
      </table>

    </div>
    <script src="./leadsController.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/js/bootstrap.min.js" integrity="sha384-vZ2WRJMwsjRMW/8U7i6PWi6AlO1L79snBrmgiDpgIWJ82z8eA5lenwvxbMV1PAh7" crossorigin="anonymous"></script>
  </body>
</html>

leadsController.js

var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope, $http) {

  $http.get("leadsData.php").then(function (response) {
      $scope.myData = response.data.records;
  });

  $scope.sortBy = function(x){
    $scope.orderByThis = x;
  };
});
  • 写回答

1条回答 默认 最新

  • duanlang1196 2018-04-27 02:14
    关注

    You can do this inside the controller itself

    $scope.sortBy = function(x){
       $scope.myData = $filter('orderBy')($scope.myData, x);
    }
    

    and make sure to inject $filter

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办