Consume Rest with AngularJS

1

I created a WebServices to get query by string and return a list, example:

url / myPath / luiz - Since luiz is the parameter that will be passed, based on this parameter will return a luiz list.

In AngularJS with you: List All, Search by ID, Delete, Change and I can not pass a parameter as a string. Here is the code snippet:

reportServices.factory('MeuServico', ['$resource',
    function($resource){
   return $resource('/url/meuPath/:nome', {}, {
    query: { method: 'GET', isArray: true },
    queryParameter: { method: 'GET',  params: {query: '@nome'} , isArray: true,} 
  });
}]);

AngularJS generates the url like this: /url/meuPath?0=%2F&1=a&2=b&3=e&4=r&5=t&6=a

As he generated in this way AngularJS discards what comes after the "?" and ends up falling on the service to list everyone.

Solution

My error occurred because of the following:

When I called the service I was doing this:

/url/meuPath/:nome

Being that I should call it like this:

/url/meuPath/nome

MeuServico.Query("Amarelão");

By doing this, the service can access the services of the service without generating the URL as if the parameter were an array

    
asked by anonymous 25.09.2014 / 16:07

2 answers

0

I did not exactly understand the problem with your code. I reproduced your code in Plunker with some changes:

link

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

app.factory('MeuServico', ['$resource', function($resource) {
  return $resource('/url/meupath/:nome', null, {
    query: {method: 'GET', isArray: true},
    queryParameter: { method: 'GET',  params: {query: '@nome'} , isArray: true}
  });
}]);

app.controller('MainCtrl', function($scope, MeuServico) {
  $scope.name = 'World';
  $scope.enviar = function() {
    $scope.name = 'Yuri';
    MeuServico.queryParameter({
      nome: 'yuri',
      grupo: 'programadores'
    });

    MeuServico.query({
      nome: 'yuri2',
      grupo: 'html'
    });
  }
});
    
22.10.2014 / 17:31
1

Apparently the problem is in

queryParameter: { method: 'GET',  params: {query: '@nome'} , isArray: true,}

The name parameter is not an array, so it should be

queryParameter: { method: 'GET',  params: {query: '@nome'} , isArray: false} 
    
06.11.2014 / 20:02