为什么要角一个监视功能JS的参数应该是一个范围变量?是一个、变量、范围、参数

2023-09-13 02:49:15 作者:故事与旧友

$scope.myFunc = function(privilege){ 

    $scope.check = function(privilege){
       return ...
    };  

    $scope.$watch('check(privilege)',function(val){
        ...
    }    
}

我的特权变量变成每个表检查(不可为)未定义?我为什么要定义它的范围变量还是我错过角的js什么?

My privilege variable becomes undefined at every watch check(which must not be)? Why I should define it scope variable or do I miss anything with Angular Js?

推荐答案

的 $看法(以下简称watchEx pression),可以是一个字符串角前pression(即在$范围评估),或功能,这被称为以$范围作为第一个参数。有没有办法通过权限作为第一个参数传递给watchEx pression这是一个功能。

The first parameter of the $watch method (the "watchExpression") can be either an Angular string expression (that is evaluated against the $scope), or a function, which is called with $scope as the first parameter. There is no way to pass privilege as the first argument to a watchExpression that is a function.

您可以创建一个关闭,如果你不想在权限值存储在$范围。请参见 Vojta开发的小提琴一个例子。另请参见 https://groups.google.com/forum/#​​!msg /角/ UJRxn_Y0Dd4 / 9ha38PC3PCwJ

You could create a closure, if you don't want to store the privilege value on the $scope. See Vojta's fiddle for an example. See also https://groups.google.com/forum/#!msg/angular/UJRxn_Y0Dd4/9ha38PC3PCwJ