将参数传递给回调函数:Scope issue
Passing the parameters to a callback function : Scope issue
我在主干应用程序的视图中有这个someFunction
。现在我想把这个函数中定义的参数发送到jConform函数的回调函数。我不知道在这种情况下scope是如何工作的。
someFunction: function() {
var thisView = this,
paramA = 10,
paramB = $(ev.currentTarget).data("id"),
paramC = this.getConfig(paramB);
jConfirm(paramC.jConfirmMessage, "Confirm Deactivation", function (ans) {
return thisView.anotherFunction(ans, paramC);
});
}
anotherFunction: function(ans, paramC){
...
}
如何将参数C从sumeFunction
访问到anotherFunction
?
谢谢!
如果您不确定:paramC
是否在-return thisView.anotherFunction(ans, thisView.paramC)
中可用。
那么答案是-是的,paramC
在该语句中可用。
原因-这里定义的回调函数会记住创建它的环境。这个想法类似于闭包。阅读这里的闭包和词法范围
但是,如果您的问题是关于如何在anotherFunction
方法的主体中访问paramC
,那么您可以按照第一个答案中的描述进行操作。或者,您可以创建视图对象的新属性,并将paramC
分配给它。
最后一件事是javascript具有功能范围。您可以在这里阅读有关javascript作用域的信息。
希望能有所帮助。
尝试这种方式
someFunction: function() {
//private
var that = this;
jConfirm(paramC.jConfirmMessage, "Confirm Deactivation", function (ans) {
return thisView.deactivateConfirm(ans, paramC);
});
return {
//public
thisView : that,
paramA : 10,
paramB : $(ev.currentTarget).data("id"),
paramC : that.getConfig(paramB)
};
}
anotherFunction: function(ans, paramC){
//use it here
}
anotherFunction(ans, someFunction.paramC);
有用的链接第一链接第二个链接
要公开内部函数和变量,需要从第一个函数返回一些内容。将它们封装在return语句中是很好的。
someFunction: function() {
var thisView = this;
thisView.paramA = 10;
thisView.paramB = $(ev.currentTarget).data("id");
thisView.paramC = this.getConfig(paramB);
return {
paramA : thisView.paramA,
paramB : thisView.paramB,
paramC : thisView.paramC,
jConfirm: function(thisView.paramC.jConfirmMessage, "Confirm Deactivation", function (ans)) {
return thisView.anotherFunction(ans, thisView.paramC)
}
}
}
anotherFunction: function(ans, paramC){
console.log(someFunction.paramC);
}
相关文章:
- AngularJS:ng之后,重复$scope值未按预期更新
- issue with FB.Event.subscribe
- $ionicplatform内的$scope不;不起作用
- 使用$scope方法时的ControllerAs语法
- 如何将ngrepeat下的ngmodel绑定到$scope
- 另一个if(!$scope.$$phase)$scope$apply()szenario-需要帮助才能通关
- 使用AngularJS Directive WHITOUT$scope创建一个动态html元素
- $scope变量被视为字符串AngularJs
- ng在更新$scope后重复不更新信息
- 正在获取Angular以检测$scope中的更改
- 如何惯用地手动销毁scope&在AngularJS中重新创建
- Understanding Javascript scope with "var that = this&qu
- AngularJS,angular.element($0).scope()揭示了每个控制器的函数
- Ng出口无法访问父$scope
- jQuery clearInterval undifined variable scope issue
- Issue With Angular $scope.Watch
- 将参数传递给回调函数:Scope issue
- ng-repeat and $scope issue
- Google Maps API - Map.data scope issue
- 在AngularJS应用程序页面加载时执行SharePoint函数($scope issue??)