javascript 角度,将作用域传递给函数(将作用域作为变量传递?)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26805471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
angular, pass a scope into a function (passing scope as variable?)
提问by ajmajmajma
I'm trying to pass a scope into a function and I can't seem to get it to work correctly. Here's what I have -
我正在尝试将作用域传递给函数,但似乎无法使其正常工作。这是我所拥有的 -
ng-click="clickFunction(scope1)"
//the function
$scope.clickFunction = function(passedScope){
passedScope = false;
console.log($scope.scope1);
So - it's prett straight forward I just want to pass in the scope and set it to false in this click. When I log the scope after changing it hwever it still says it's true. I also tried -
所以 - 这很直接,我只想传入范围并在这次点击中将其设置为 false。当我在更改范围后记录范围时,它仍然说它是真的。我也试过——
$scope.passedScope
What I am trying to do is set $scope.scope1 = false. It is set in the top of the controller as true and controls a button nearby by that button having ng-disabled="!scope1", I cant just do a scope1 =!scope! on the click because it goes through a modal to confirm the user wants to complete then runs a modalInstance.result.then(function () { there I then need to set the passed scope to false. I would just call the scope directly, but I'm trying to make a function which I can use across multiple delete functions, thus trying to pass he scope that needs changing to false.
我想要做的是设置 $scope.scope1 = false。它在控制器顶部设置为 true 并控制该按钮附近的按钮,该按钮具有 ng-disabled="!scope1",我不能只做一个 scope1 =!scope! 点击因为它通过一个模态来确认用户想要完成然后运行一个 modalInstance.result.then(function () { 然后我需要将传递的范围设置为 false。我只是直接调用范围,但是我正在尝试创建一个可以跨多个删除函数使用的函数,从而尝试传递需要更改为 false 的范围。
I was thinking I could just pass the scope through the function.
我在想我可以通过函数传递范围。
Thanks!
谢谢!
updateAccording to what @Josep showed me today I was able to make a work around by passing the scope as a string like so
更新根据@Josep今天向我展示的内容,我能够通过将范围作为字符串传递来解决问题
ng-click="clickFunction('scope1')"
and then doing
然后做
$scope[passedScope] = false;
回答by Josep
If you want to pass the current scope of a part of your view to a function, the way to do it would be:
如果要将视图的一部分的当前作用域传递给函数,则这样做的方法是:
ng-click="clickFunction(this)"
But then in your function you should be treating that scope like this:
但是在您的函数中,您应该像这样处理该范围:
$scope.clickFunction = function(passedScope){
passedScope.somePropertyOfTheScope = false;//<---Notice the difference with the code that you are suggesting.
You can't set the scope
to false
, you can set one of its properties to false
, but not the scope itself. That wouldn't make any sense because scope
is an object that holds a set of properties, events and functions. Some of them inherited from its scope ancestors up to $rootScope
, and some of them are custom.
您不能将 设置scope
为false
,您可以将其属性之一设置为false
,但不能将范围本身设置为。这没有任何意义,因为它scope
是一个拥有一组属性、事件和函数的对象。其中一些是从其作用域祖先继承而来的$rootScope
,有些是自定义的。
Maybe what you are trying to do is to pass one of the properties of the scope
as a parameter of a function, so that you can change it in that function. However, in javascript the only way to pass a parameter by reference is to pass the object and update one of its properties, if you pass a boolean property to the function, then you will be passing the value of that boolean property, not the reference. Please have a look at this: Pass Variables by Reference in Javascript.
也许您想要做的是将 的属性之一scope
作为函数的参数传递,以便您可以在该函数中更改它。但是,在 javascript 中,通过引用传递参数的唯一方法是传递对象并更新其属性之一,如果您将布尔属性传递给函数,那么您将传递该布尔属性的值,而不是引用. 请看看这个:在 Javascript 中通过引用传递变量。
回答by falsarella
As an alternative to Josep's answer(which helped me find it out), you could simply call like this:
作为Josep 答案的替代方案(帮助我找到了答案),您可以简单地这样调用:
ng-click="clickFunction()"
And then in your function you can refer the current scope with this
:
然后在您的函数中,您可以使用以下命令引用当前范围this
:
$scope.clickFunction = function() {
this.somePropertyOfTheScope = false;
//...
}
This is very useful when using ng-include
that creates a new scope, so clickFunction
is actually at parent scope.
这在使用ng-include
它创建新范围时非常有用,因此clickFunction
实际上是在父范围内。