在 angular 服务中使用调用和绑定
using call and bind in an angular service
我目前正在学习 angularjs,我想知道一些事情。
用户如何调用或申请将回调函数的值连接到服务中的值。让我解释一下。
app.service("AppService"), function(){
//value to be bound
this.value;
//function that references a promise or something
this.doThing = function(){
//refer to a promise
externalPromise("string").then(function(response){
//The scope has changed so "this" is no longer refering to the service
this.value = response;
});
}
})
app.controller("AppCtrl", function(AppService){
alert(AppService.value);
})
我知道这可以(而且可能应该)可以完成:
app.service("AppService"), function(){
//value to be bound
var value;
//function that references a promise or something
var doThing = function(){
//refer to a promise
externalPromise("string").then(changeValue(response));
}
function changeValue(response){
value = response;
}
var getValue = function(){return value}
return {
value: getValue,
doThing: doThing
}
})
app.controller("AppCtrl", function(AppService){
alert(AppService.value);
})
但是如果服务的重点是它们 return "this" 那么我认为利用它是最有意义的。我认为可以使用 call bind 或 apply 将 changeValue 函数内的 this
设置为与控制器中的 this
相同。我无法弄清楚究竟如何。有人知道吗?即使没有必要,也可以将其视为学术练习。
编辑:另一个问题中提出的解决方案有效且可行。但是我想知道 angular 中是否有特定的方法可以做到这一点。我标记为正确的答案建议在我想绑定的函数上使用 angular.bind()
。
您应该能够使用 angular.bind 为您的处理函数提供正确的 "this":
this.doThing = function(){
externalPromise("string").then(angular.bind(this, function(response){
//this is now the "this" you expect
this.value = response;
}));
}
一种常用的替代方法是将 "this" 存储在一个变量中,然后在您的处理程序中使用该变量:
this.doThing = function(){
var self = this;
externalPromise("string").then(function(response){
//self is now the "this" you wanted.
self.value = response;
});
}
我目前正在学习 angularjs,我想知道一些事情。 用户如何调用或申请将回调函数的值连接到服务中的值。让我解释一下。
app.service("AppService"), function(){
//value to be bound
this.value;
//function that references a promise or something
this.doThing = function(){
//refer to a promise
externalPromise("string").then(function(response){
//The scope has changed so "this" is no longer refering to the service
this.value = response;
});
}
})
app.controller("AppCtrl", function(AppService){
alert(AppService.value);
})
我知道这可以(而且可能应该)可以完成:
app.service("AppService"), function(){
//value to be bound
var value;
//function that references a promise or something
var doThing = function(){
//refer to a promise
externalPromise("string").then(changeValue(response));
}
function changeValue(response){
value = response;
}
var getValue = function(){return value}
return {
value: getValue,
doThing: doThing
}
})
app.controller("AppCtrl", function(AppService){
alert(AppService.value);
})
但是如果服务的重点是它们 return "this" 那么我认为利用它是最有意义的。我认为可以使用 call bind 或 apply 将 changeValue 函数内的 this
设置为与控制器中的 this
相同。我无法弄清楚究竟如何。有人知道吗?即使没有必要,也可以将其视为学术练习。
编辑:另一个问题中提出的解决方案有效且可行。但是我想知道 angular 中是否有特定的方法可以做到这一点。我标记为正确的答案建议在我想绑定的函数上使用 angular.bind()
。
您应该能够使用 angular.bind 为您的处理函数提供正确的 "this":
this.doThing = function(){
externalPromise("string").then(angular.bind(this, function(response){
//this is now the "this" you expect
this.value = response;
}));
}
一种常用的替代方法是将 "this" 存储在一个变量中,然后在您的处理程序中使用该变量:
this.doThing = function(){
var self = this;
externalPromise("string").then(function(response){
//self is now the "this" you wanted.
self.value = response;
});
}