为什么我不能通过AngularJS的$ location.search()作为回调?

时间:2019-03-26 20:53:02

标签: angularjs

我有类似的东西:

conf

还有更多的东西,但这是失败的部分。

我可以这样称呼它:

function x(hash, f) {
    Object.keys(hash).forEach(key => {
        f(key, hash[key]);
    });
}

但是当我尝试简化为

x(h, function(n, v) { $location.search(n,v) });

它在x(h, $location.search); 中炸毁

x

如果我逐步进入调试器,则在输入angular.js:13920 TypeError: Cannot read property '$$search' of undefined at search (angular.js:13337) at x (parameters.js:120) f看起来像一个函数。有什么办法解决这个问题,还是我需要那个丑陋的包装纸?

1 个答案:

答案 0 :(得分:1)

函数调用失败,因为this上下文是undefinedsearch函数期望绑定到$location对象。

呼叫可以简化为:

x(h, $location.search.bind($location));

DEMO on PLNKR