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



我有这样的东西:

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)

如果我在调试器中单步执行,当我输入 x 时,f看起来像一个函数。 有没有办法解决这个问题,或者我需要那个丑陋的包装纸?

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

调用可以简化为:

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

PLNKR上的演示

最新更新