Skip to content

Commit 1b66e0f

Browse files
committed
Ignore parameters starting with digit in $resource
Currently URLs like http://example.com:8080/some/path do not work with $resource without tricks because port number :8080 is handled as path parameter. This commit changes $resource to ignore parameters that start with a digit. Fixes angular#1243.
1 parent 861e0c7 commit 1b66e0f

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/ngResource/resource.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@
3030
* and you are ready to get started!
3131
*
3232
* @param {string} url A parametrized URL template with parameters prefixed by `:` as in
33-
* `/user/:username`. If you are using a URL with a port number (e.g.
34-
* `http://example.com:8080/api`), you'll need to escape the colon character before the port
35-
* number, like this: `$resource('http://example.com\\:8080/api')`.
33+
* `/user/:username`.
3634
*
3735
* If you are using a url with a suffix, just add the suffix, like this:
3836
* `$resource('http://example.com/resource.json')` or `$resource('http://example.com/:id.json')
@@ -346,7 +344,9 @@ angular.module('ngResource', ['ng']).
346344
var urlParams = self.urlParams = {};
347345
forEach(url.split(/\W/), function(param){
348346
if (param && (new RegExp("(^|[^\\\\]):" + param + "(\\W|$)").test(url))) {
349-
urlParams[param] = true;
347+
if(new RegExp("^[^0-9].*$").test(param)) {
348+
urlParams[param] = true;
349+
}
350350
}
351351
});
352352
url = url.replace(/\\:/g, ':');

test/ngResource/resourceSpec.js

+22
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,28 @@ describe("resource", function() {
7373
R.get({a:6, b:7, c:8});
7474
});
7575

76+
it('should ignore parameters starting with digit', function() {
77+
var R = $resource('http://www.example.com:8080/Path/:4a/:a');
78+
79+
$httpBackend.when('GET', 'http://www.example.com:8080/Path/:4a').respond('{}');
80+
$httpBackend.when('GET', 'http://www.example.com:8080/Path/:4a/0').respond('{}');
81+
$httpBackend.when('GET', 'http://www.example.com:8080/Path/:4a?8080=1').respond('{}');
82+
83+
R.get({});
84+
R.get({a:0});
85+
R.get({"8080":1});
86+
});
87+
88+
it('should not ignore parameters that contain digits but do not start with digit', function() {
89+
var R = $resource('/Path/:a42');
90+
91+
$httpBackend.when('GET', '/Path').respond('{}');
92+
$httpBackend.when('GET', '/Path/7').respond('{}');
93+
94+
R.get({});
95+
R.get({a42: 7});
96+
});
97+
7698
it('should not ignore leading slashes of undefinend parameters that have non-slash trailing sequence', function() {
7799
var R = $resource('/Path/:a.foo/:b.bar/:c.baz');
78100

0 commit comments

Comments
 (0)