AngularJS resource - encoding url string parameter as an array

前提是你 提交于 2019-12-11 06:58:49

问题


I'm getting this weird url from Angular's $resource:

DELETE /dashboard/api/1.0/journalEntry?0=5&1=4&10=9&11=3&12=2&13=f&14=7&15=f&16=1&17=6&18=1&19=4&2=0&20=9&21=2&22=a&23=a&3=7&4=9&5=6&6=e&7=e&8=5&9=f

When what I'm really expecting is this:

DELETE /dashboard/api/1.0/journalEntry/540796ee5f932f7f161492aa

I've tried that URL and the server side appears to be working. It's a problem with the Angular Router Service.

Here's where I define the service:

'use strict';
angular.module('mean.dashboard')
.factory('JournalEntry', ['$resource',
  function($resource){
    return $resource('dashboard/api/1.0/journalEntry/:journalEntryId/', {
      journalEntryId: '@_id'
    }, {
      update: {
        method: 'PUT'
      }
    });
  }
]);

And here's the controller where I call Resource:

$scope.editDeleteRow = function(item) {
  console.log(item._id);
  JournalEntry.remove(item._id).$promise.then(
    function(response) {
      $scope.addAlert('edit', 'Deleted', 'success');
      //TODO remove item from client list
      $scope.groupJournalEntries();
    },
    function(err) {
      $scope.addAlert('add', 'Error ' + err.statusText, 'danger');
      console.log(err);
    }
  );
};

What I miss?


回答1:


I'm in a hurry so I didn't figure this one out all the way. It had something to do with @ not working the way I expected. I got it to work defining the service like this:

.factory('JournalEntry', ['$resource',
  function($resource){
    return $resource('dashboard/api/1.0/journalEntry/:journalEntryId', {
    }, {
      update: {
        method: 'PUT'
      }
    });
  }
]);

And calling it like this:

  JournalEntry.remove({journalEntryId: item._id}).$promise.then(


来源:https://stackoverflow.com/questions/25657056/angularjs-resource-encoding-url-string-parameter-as-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!