angularjs - ngResource GET with filter values -
i'm writing small test application can retrieve customers parse.com database.
i have following form in html
... <body ng-app="myapp"> <div ng-controller="customercontroller"> <button ng-click="getcustomers()">get customers</button> <ul> <li ng-repeat="customer in customers">{{ customer.name }}</li> </ul> </div> </body> ...
my angular app following:
module
var app = angular .module('myapp', ['ngresource']) .constant('myconfig', { 'api_url': 'https://api.parse.com/1/classes/', 'parse_application_id': 'xxxxxxxxxxxxx', 'parse_rest_api_key': 'xxxxxxxxxxxxx' });
factory
app.factory('customersservice', function($resource, myconfig) { return $resource(myconfig.api_url + 'customer', {}, { query: { method: 'get', isarray: false, headers: { 'x-parse-application-id': myconfig.parse_application_id, 'x-parse-rest-api-key': myconfig.parse_rest_api_key } }, create: { method: 'post', headers: { 'x-parse-application-id': myconfig.parse_application_id, 'x-parse-rest-api-key': myconfig.parse_rest_api_key } } }) });
controller:
app.controller('customercontroller', function($scope, customersservice, customerservice) { $scope.getcustomers = function() { customersservice.query().$promise.then(function(result) { $scope.customers = result.results; }); }; });
so when click button, works should. want add filter name when want retrieve customers database. when execute following in postman
https://api.parse.com/1/classes/customer?where={"name":"aaaaa"}
this works , gets customer name "aaaaa". know syntax ok.
so add textbox user can enter customername , after want click on search button. how can manage ?where={"name":"aaaaa"} angular stuff when click button? want expand filter other columns customer.
something should work (assuming goes in where
object)
add search fields bind scoped object's properties. we'll call search
<label for="search_name">name</label> <input type="text" ng-model="search.name" name="name" id="search_name"> <label for="search_city">city</label> <input type="text" ng-model="search.city" name="city" id="search_city">
then can execute query action with
customersservice.query({where: $scope.search}).$promise...
that should create query param like
?where=%7b%22name%22%3a%22aaaaa%22%2c%22city%22%3a%22london%22%7d
which uri encoded value
?where={"name":"aaaaa","city":"london"}
Comments
Post a Comment