Can I set a template for $ httpBackend responses?

Suppose I have the following test code in AngularJS:

var someURL;
var dummyJSON;
$httpBackend.whenGET(someURL).respond(dummyJSON);

Is there a way to make this answer for a set of URLs, not just one? For example, I would like it to respond with the same JSON dummy for ANY URL that starts with / api /, but not with those starting with / app /. How is the wildcard URL ("/ app / *")?

thanks for the help

+4
source share
2 answers

I got lucky with type expressions whenGET(/^\/api\//)- that means it starts with /api/. In the regex, it ^will match the beginning of the line, and \/will match the literal /in the url.

, whenGET(/\/api\//), URL-, URL-.

+7

, . - :

$httpBackend.when("GET", function(url) {return url.indexOf("/api/") === 0;}
+4

All Articles