简体   繁体   English

angular,将范围传递给函数(将范围作为变量传递?)

[英]angular, pass a scope into a function (passing scope as variable?)

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. 所以 - 它很直接我只想传入范围并在此单击中将其设置为false。 When I log the scope after changing it hwever it still says it's true. 当我更改它后记录范围时,它仍然说它是真的。 I also tried - 我也尝试过 -

  $scope.passedScope

What I am trying to do is set $scope.scope1 = false. 我想要做的是设置$ 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! 它在控制器的顶部设置为true,并通过具有ng-disabled =“!scope1”的按钮控制附近的按钮,我不能只做一个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. 在点击,因为它通过一个模态来确认用户想要完成然后运行modalInstance.result.then(function(){然后我需要将传递的范围设置为false。我只是直接调用范围,但我正在尝试创建一个可以在多个删除函数中使用的函数,因此尝试将需要更改为false的范围传递给它。

I was thinking I could just pass the scope through the function. 我以为我可以通过该功能传递范围。

Thanks! 谢谢!

update According 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;

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. 您不能将scope设置为false ,您可以将其中一个属性设置为false ,但不能将范围本身设置为false That wouldn't make any sense because scope is an object that holds a set of properties, events and functions. 这没有任何意义,因为scope是一个包含一组属性,事件和函数的对象。 Some of them inherited from its scope ancestors up to $rootScope , and some of them are custom. 其中一些从其范围祖先继承到$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 . 也许您要做的是将scope一个属性作为函数的参数传递,以便您可以在该函数中更改它 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. 但是,在javascript中,通过引用传递参数的唯一方法是传递对象并更新其属性之一,如果将布尔属性传递给函数,那么您将传递该布尔属性的值,而不是引用。 Please have a look at this: Pass Variables by Reference in Javascript . 请看一下: 在Javascript中通过引用传递变量

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实际上位于父范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM