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 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题