Search code examples
angularjsangular-resource

Passing Domain Name as parameter with $resource in AngularJS


I want to pass a domain name as parameter with $resource request. I tried this but it doesn't work. I don't see why. Any clue ? It outputs http://:url/ instead of the variable I'm trying to pass.

edit: the variable :url passes if I do something like this : http://adomain.com/:url

Here's my code :

My factory :

angular.module('myApp')
  .factory('LoadingContent', function LoadingContentFactory($resource) {
    return $resource('http://:url/wp-json/posts/?type[]=:type&filter[posts_per_page]=50&filter[order]=DESC', {type: '@type',url: '@url'}, {'get': {method: 'GET', isArray: true, params: {type: '@type',url: '@url'} }});
  });

My function :

LoadingContent.get({
    url : $scope.selectedCompany.URL,
    type : $scope.selectedCompany.type,
})
.$promise.then(
    function(data){
        $scope.articles = data;
        openInfoModals.closeModal();
    }
);

Problem solved : The problem comes from the version of angular-resource#1.4.5. I went back to 1.4.3 version and it works.

If anyone has a solution to make it work with 1.4.5, he's welcome.


Solution

  • The real issue stems from adding support for IPv6 URLs in ngResource (see https://github.com/angular/angular.js/issues/12512). If you take a look at the commit, they effectively ignored the domain/hostname when doing parameter replacement. As mentioned, downgrading to 1.4.3 fixes the issue. Fortunately, that seems to be the only change between 1.4.3 and 1.5.5, so downgrading isn't too much of a problem.

    For completeness, I also created this issue in the angular project to track a permanent fix for it: https://github.com/angular/angular.js/issues/14542.