在下面的示例中,$http.get().success()
调用中的上下文是undefined
. 我想这是因为我使用“使用严格;” 这success()
是一个常规功能。
但是我需要在函数调用中访问服务的上下文。实现这一目标的正确方法是什么?
ng_app.service('database', function($http)
{
this.db = new Db();
this.load = function()
{
console.log(this); // logs the service context correctly
$http.get('get_nodes/').success(function(ajax_data)
{
console.log(this); // logs "undefined"
console.log(this.db); // throws an exception because this is undefined
this.db.nodes = ajax_data; // throws an exception because this is undefined
});
}
});