是否有可能设置$ httpBackend反应通配符? [英] Is it possible to set a wildcard for $httpBackend responses?
问题描述
假设我有下面的测试$ C $在AngularJS C:
Suppose I have the following test code in AngularJS:
var someURL;
var dummyJSON;
$httpBackend.whenGET(someURL).respond(dummyJSON);
有没有使这个一组网址,而不是只有一个响应的一种方式?
例如,我想它使用相同的虚拟JSON作出响应以/ API /开头的网址,而不是那些用/程序/启动。就像一个通配符URL(/应用程序/ *)?
Is there a way of making this the response for a set of URLs rather than just one? For example, I'd like it to respond with the same dummy JSON for ANY url that starts with /api/, but not ones that start with /app/. Like a wildcard URL ("/app/*") ?
感谢您的帮助。
推荐答案
我已经与前pressions像 whenGET运气(/ ^ \\ / API \\ //)
- 这意味着 / API /
启动。在正则表达式, ^
将匹配字符串的开头和 \\ /
将匹配一个 /
在URL中。
I've had luck with expressions like whenGET(/^\/api\//)
– which means starts with /api/
. In the regex, ^
will match the start of the string and \/
will match a literal /
in the URL.
如果不符合您的要求,请尝试 whenGET(/ \\ / API \\ //)
这应该与绝对的URL以及相对URL。
If that doesn't match your requests, try whenGET(/\/api\//)
which should match absolute URLs as well as relative URLs.
这篇关于是否有可能设置$ httpBackend反应通配符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!