且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

棱角分明,通过一个范围到一个函数(传递范围为变量?)

更新时间:2023-01-08 17:44:14

如果你想通过你的视图功能的一部分,目前范围,方式做这将是:

  NG-点击=clickFunction(本)

但随后在你的函数,你应该是治疗范围如下:

  $ scope.clickFunction =功能(passedScope){
   passedScope.somePropertyOfTheScope = FALSE; //< ---注意与code,你所提出的建议的区别。

您不能设置范围 ,可以将其中一个它的属性,而不是本身的范围。这将没有任何意义,因为 范围 是用于保存一组属性,事件和函数的对象。他们中有些人继承其祖先范围高达 $ rootScope ,其中有些是定制。

也许你正在试图做的是要通过范围的属性之一作为函数的参数,这样就可以在改变该函数。然而,在JavaScript中的唯一的方式来传递参数通过引用传递对象,并更新其属性之一,如果你传递一个布尔属性的功能,那么你将被传递一个布尔值属性的值,而不是引用。请看看这个:通过在Javascript 参考传递变量

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 -

  $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.

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

   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. 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.

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.