为什么要角一个监视功能JS的参数应该是一个范围变量? [英] Why Angular Js Parameter of a Watch Function Should Be a Scope Variable?

查看:109
本文介绍了为什么要角一个监视功能JS的参数应该是一个范围变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$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

这篇关于为什么要角一个监视功能JS的参数应该是一个范围变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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