Utilizing array-style query parameters for resource requests in AngularJS

I am currently faced with the challenge of working with an API that utilizes array style query parameters for filtering items. Unfortunately, I am struggling to implement this in Angular.

For instance, the API endpoint requires a URL structure like:

example.com/api/list?filter[number]=1

In my current setup, I have a dropdown menu that assigns the selected value to the list of parameters and triggers a filter method. Typically, this process is straightforward when dealing with regular key-value pairs. However, in this case, the format required by the API complicates things.

$scope.paramers = {
    include: 'playing', 
    sort: '-id'
};
$scope.refresh = function () {
    LFGFactory.query($scope.paramers, function success (response) {
        $scope.loading = true;
        var data = response.data;
        if (data.length >= 1) {
            $scope.rowList = data;
            $scope.loading = false;
        } else {
            $scope.loading = false;
        }
    },
    function err (data) {
        console.log(data);
    });
};

The selection options in my view are as follows:

        <div class="form-group pull-right">
            <select id="plat-sel" name="plat-sel" class="form-control" ng-model="paramers.filter" ng-change="refresh()">
                <option value="" disabled selected>Filter by Platform</option>
                <option value="1183">Xbox One</option>
                <option value="1184">PlayStation 4</option>
                <option value="1182">PC</option>
                <option value="1188">Wii U</option>
                <option value="1186">Xbox 360</option>
                <option value="1185">PlayStation 3</option>
            </select>
        </div>

Here is the factory code:

  .factory('LFGFactory', function($resource) {

    var base = 'http://example.com/api/v1.0/';


    return $resource(base +'lfg', {},
        {
          update: {
            method: 'PUT',
            isArray: true
          },
          delete: {
            method: 'DELETE',
            isArray: true
          },
          query: {
            method: 'GET',
            isArray: false
          }
        }
    );
  }) 

While simply adding filter:'1' to the existing $scope.parameters object would suffice under normal circumstances, I need to figure out how to add filter[number] = 1. How can I achieve this using ng-model and my current setup?

Answer №1

After examining your LFGFactory service:

angular.module('myApp').factory('LFGFactory', function($resource) { 
      var base = 'sample.com/api/v1.0/';
      return $resource(base +'lfg', {}, 
            { update: { method: 'PUT', isArray: true }, 
              delete: { method: 'DELETE', isArray: true }, 
              query:  { method: 'GET', isArray: false } } 
      );

}) 

You have implemented ngParamSerializer

Please update your select element to:

 <select id="plat-sel" name="plat-sel" class="form-control" 
          ng-model="paramers['filter[number]']" ng-change="refresh()">

The JSFiddle link

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I am curious about the distinction between two closures

Can someone please explain the distinction between these two closure examples? (function(window, undefined) { // JavaScript code })(window); Here's another example: (function(window) { // JavaScript code })(window, undefined); ...

Make sure that the iframe loads the next page with enough force to break out

My dilemma involves an iframe that loads the new tab page. When a user clicks on the thumbnail, it opens within the iframe. My goal is to have any subsequent load in the iframe redirected to window.top. Is there a way to achieve this without manually setti ...

Incorporating a new div element into a CSS layout with multiple

Could this be done? Imagine I have 15 divs, with 3 in each of the 5 columns of a multiple column CSS layout. I also have responsive code that adjusts the number of columns based on screen size. So, is there a way to insert a div between the 12th and 13th ...

Browsing a Table in Vue

I'm currently building a Vue application that showcases a collection of quotes and includes a search feature for filtering through the data. It seems like I might not have properly linked the function to the correct element. I've added a v-model ...

Tips for creating modular Angular controllers

Feel free to modify the title as needed. I am familiar with writing controllers in a modular manner. var controllers = {}; controllers.ToDoController = function($scope){ //... }; Alternatively, you can achieve the same results using: var app = angul ...

Having trouble with yarn install? Keep receiving the error message "Other managers are not allowed"?

Recently, I began using the yarn package manager for one of my projects. To get started, I globally installed yarn using sudo npm install yarn -g. However, when attempting to install dependencies with yarn install, I encountered the following message on t ...

I am struggling to comprehend the code for a PHP form that triggers a JS OnUpdate event, which then uses AJAX to retrieve data from MySQLi

I want to give a special thanks to Alon Alexander for providing the JS and AJAX code, even though I don't fully comprehend it. I am more comfortable using PHP/JS without jQuery, but I am struggling to make it function as intended. My current issue in ...

"Error" - The web service call cannot be processed as the parameter value for 'name' is missing

When using Ajax to call a server-side method, I encountered an error message: {"Message":"Invalid web service call, missing value for parameter: \u0027name\u0027.","StackTrace":" at System.Web.Script.Services.WebServiceMethodData.CallMethod(O ...

Minimize all expanded containers in React

Although I've come across similar questions, none of them have addressed mine directly. I managed to create a collapsible div component that expands itself upon click. However, I am looking for a way to make it so that when one div is expanded, all o ...

Tips for integrating the AJAX response into a Sumo Select dropdown menu

I am currently using Sumoselect for my dropdowns, which can be found at . The dropdowns on my page are named as countries, state, and cities. The countries are shown in the dropdown, and based on the country selected, the corresponding state name should a ...

Having trouble with the JQuery .on() method not triggering?

Currently, I am attempting to utilize the JQuery on() method in order to generate an alert box similar to the voting system on certain websites. If you've ever tried to vote on your own comment or thread, you'll understand what I mean. I have cr ...

Steps for importing a React component as an embedded SVG image

I have developed a SVG component in React by converting an SVG file to a React component using the svg-to-react cli tool. In order to load and display additional svg files within this component, I am utilizing the SVG image tag as demonstrated below. This ...

Guide on adding a new member to Mailchimp through node.js and express

Hello, I've been delving into working with APIs, particularly the mail-chimp API. However, I've encountered a problem that has me stuck: const express=require("express"); const bodyparser=require("body-parser"); const request=require("request" ...

Best practices for displaying a Multidimensional JSON Object using JavaScript

Within my current project, I have a JSON object structured as follows: { "face": [ { "attribute": { "age": { "range": 5, "value": 35 }, "gender": { "confidence ...

How to customize text within an HTML <option> tag within a <select> element

Customizing the appearance of items in an option list can be easily achieved by using CSS: <select> <option>Option 1</option> <option style="color: #F00; font-weight: bold; padding-left:2em;">Option 2</option> <opt ...

Essential Guide to Binding Events with JQuery

Why is the handler for an event on an element triggering the wrong response? I was expecting the click event of Div1 to display a dialog stating 'div1', but it's showing 'div2' instead. I am new to this and trying to figure out wh ...

A dynamic AJAX menu showcasing content in a dropdown format, away from the conventional table layout

My dropdown select menu is correctly populating with data, but the output always appears outside of the table. Can anyone spot the issue in my code? Any suggestions or ideas are greatly appreciated! Thanks in advance, select.php <?php $q = $_GET[&apos ...

What is the best way to customize the link style for individual data links within a Highcharts network graph?

I am currently working on creating a Network Graph that visualizes relationships between devices and individuals in an Internet of Things environment. The data for the graph is extracted from a database, including information about the sender and receiver ...

Tips on how to navigate a specific div to the left in Selenium?

During my testing of an Angular page, there was a need to scroll a specific segment to the left. I attempted using the code below: JavascriptExecutor jse = (JavascriptExecutor) driver; jse.executeScript("window.scrollBy(200,200)", ""); Unfortunately, the ...

Utilizing SASS, JavaScript, and HTML for seamless development with Browser Sync for live syncing and

I've been on a quest to find a solution that covers the following requirements: Convert SASS to CSS Post-process CSS Minify CSS Move it to a different location Bundle all Javascript into one file Create compatibility for older browsers Tre ...