如何过滤GET请求?

时间:2018-01-18 21:28:10

标签: javascript angularjs http get odata

使用网络请求,我正在检索数据并将其显示在网页上。我不想使用Angular的ng-if并隐藏不符合条件的数据,而是根本不想检索数据。

JS:

var app = angular.module('myApp', ['ngSanitize']);
app.controller('MainCtrl', function($scope, $http, $q){
  $(document).ready(function() {
    $scope.getAdminList();
  });
    $scope.prepContext = function(url,listname,query){
        var path = url + "/_api/web/lists/getbytitle('" + listname + "')/items" + query;
        console.log(path);
        return path;
    }
    $scope.getAdminList = function() {
    adminList = $http({
      method: 'GET',
      url: this.prepContext(siteOrigin+"/corporate/projecthub/anchormn/associates","User Administration","?$orderBy=LastName"),
      headers: {
        "Accept": "application/json; odata=verbose"
      }
    }).then(function(data) {
      //$("#articleSection").fadeIn(2000);
      console.log("adminlist", data.data.d.results);
      $scope.users = data.data.d.results;
    });
  };
});

记录data.data.d.results;记录一个类似于:

的对象数组
{
  0: {
    "ID": 21,
    "Name": Me
    },
  1: {
    "ID": 14,
    "Name": Test
    },
  2: {
    "ID": 3,
    "Name": Test1
    }
}

如何仅使用网络请求检索项目,而不是使用ng-if="user.ID == 21

3 个答案:

答案 0 :(得分:1)

从您的请求标头和URI($ orderBy),我了解您的服务器是OData服务器。如果服务器正确实现了过滤器,您可以使用OData $过滤器表达式作为查询字符串的一部分,类似于$ orderBy。

示例:

GET Products?$filter=ProductName+eq+%27iPhone%27

请检查网址:http://www.odata.org/documentation/odata-version-2-0/uri-conventions/

Logical Operators

Eq  Equal   /Suppliers?$filter=Address/City eq 'Redmond'
Ne  Not equal   /Suppliers?$filter=Address/City ne 'London'
Gt  Greater than    /Products?$filter=Price gt 20
Ge  Greater than or equal   /Products?$filter=Price ge 10
Lt  Less than   /Products?$filter=Price lt 20
Le  Less than or equal  /Products?$filter=Price le 100
And Logical and /Products?$filter=Price le 200 and Price gt 3.5
Or  Logical or  /Products?$filter=Price le 3.5 or Price gt 200
Not Logical negation    /Products?$filter=not endswith(Description,'milk')

Arithmetic Operators
Add Addition    /Products?$filter=Price add 5 gt 10
Sub Subtraction /Products?$filter=Price sub 5 gt 10
Mul Multiplication  /Products?$filter=Price mul 2 gt 2000
Div Division    /Products?$filter=Price div 2 gt 4
Mod Modulo  /Products?$filter=Price mod 2 eq 0

Grouping Operators
( ) Precedence grouping /Products?$filter=(Price sub 5) gt 10

答案 1 :(得分:0)

$scope.users = (data.data.d.results).filter(function(user) {
    return user.ID === 21;
});

答案 2 :(得分:0)

Using ngResource (query) which is also GET request and using ControllerAS syntax:    
        var app = angular.module('myApp', [ 'ngResource' ]);
        app.factory('userOrderID', function($resource) {
            return $resource(
                    '/URL/:userID ', {
                        userID : '@uID'
                    });
        });
        app.controller('ExampleController', ExampleController);
        function ExampleController(userOrderID) {
        //findtheResult function being called from HTML on click of search button
            this.findtheResult = function() {
                this.userDetails = [];
                this.userDetails = userOrderID.query({
                        userID : this.userID //passed from html page as input type
                    }, function(data) {
                        if (data.length == 0) {
                            alert("No Master ID found in database");
                        }
                    });
        };
        };