棱角分明,通过一个范围到一个函数(传递范围为变量?) [英] angular, pass a scope into a function (passing scope as variable?)

查看:97
本文介绍了棱角分明,通过一个范围到一个函数(传递范围为变量?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个范围传递到一个功能,我似乎无法得到它的正常工作。下面是我的 -

  NG-点击=clickFunction(scope1)//功能
$ scope.clickFunction =功能(passedScope){      passedScope = FALSE;
      的console.log($ scope.scope1);

所以 - 这是preTT直截了当我只想范围传递,并将其设置为false,在这个点击。当我改变它hwever后登录的范围还在说这是真的。我也尝试 -

  $ scope.passedScope

我所试图做的是设置$ scope.scope1 = FALSE。它是在控制器顶部设置为真,并有NG-禁用=!scope1该按钮控制附近的一个按钮,我不能只是做一个scope1 =!范围内!在点击,因为它通过一个模式来确认用户想要完成然后运行modalInstance.result.then(函数(){有那么我需要设置传递范围为假。我只是直接调用的范围,但我试图让我可以在多个删除的功能使用功能,从而试图通过他的范围,需要改变为false。

我想我可能只是通过函数传递的范围。

谢谢!

更新
根据什么@Josep今天给我看,我能够通过传递的范围作为像这样的字符串,使周围的作品

  NG-点击=clickFunction('scope1')

,然后做

  $范围[passedScope] = FALSE;


解决方案

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

  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.

这篇关于棱角分明,通过一个范围到一个函数(传递范围为变量?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆