简体   繁体   English

绑定到AngularJS中的函数在幕后会发生什么

[英]What happens behind the scenes when binding to a function in AngularJS

Can anyone explain what happens behind the scenes when you bind to a function in AngularJS? 当您绑定到AngularJS中的函数时,谁能解释幕后发生的事情? What kind of watch does it create? 它创造出什么样的手表? I have a feeling it would create two watches (in my example below) one for each property that makes up the return value. 我感觉它将为组成返回值的每个属性创建两个手表(在下面的示例中)。 However I'm certainly not sure about this but it feels like something we shouldn't do. 但是我对此不确定,但是感觉我们不应该这样做。

eg 例如

<div ng-show="vm.someFunc()">

JS JS

vm.someFunc = function() {  
    return vm.property1 || vm.property2;
}

If you created the angular scope method "vm.someFunc()", this will continuously get polled. 如果创建了角度范围方法“ vm.someFunc()”,则将连续对其进行轮询。 You can verify this by setting a breakpoint in this method, it will continuously keep getting hit. 您可以通过在此方法中设置断点来验证这一点,它将不断受到攻击。 If you check the task manager and show the browser running your website, the memory keeps going up and won't stop. 如果您检查任务管理器并显示浏览器正在运行您的网站,则内存会不断增加并且不会停止。

In my opinion, scope functions should only be used when using event triggers: click event, change event, keypressed are some of the examples. 我认为,作用域函数仅应在使用事件触发器时使用:单击事件,更改事件,按键是其中的一些示例。

Showing or hiding aren't events, so this is why it gets polled like that.To fix and provide the same functionality, turn this into a scope variable. 显示或隐藏不是事件,所以这就是为什么要对其进行轮询。要修复并提供相同的功能,请将其转换为作用域变量。
change the html tag from: 从以下位置更改html标签:

<div ng-show="vm.someFunc()">

to

<div ng-show="vm.someFunc">

And in your controller: 在您的控制器中:

$scope.KeyPressed = false;
$scope.Tags = '';

then create a watch event on what you want to watch for: 然后针对您要观看的内容创建一个观看事件:

//initialize showtag when page loads
$scope.vm.someFunc = $scope.KeyPressed && $scope.Tags !== '';

//watch for any new changes on keypressed
$scope.$watch('KeyPressed', function (newValue, oldValue) {
     if (newValue && $scope.Tags !== '') {
         $scope.vm.someFunc= true;
     } else {
         $scope.vm.someFunc= false;
     }
}

//watch for any new changes on keypressed
$scope.$watch('Tags', function (newValue, oldValue) {
     if (newValue !== "" && $scope.KeyPressed) {
         $scope.vm.someFunc= true;
     } else {
         $scope.vm.someFunc= false;
     }
}

Or you can change to a "watchCollection" instead of having multiple watches like: 或者,您可以更改为“ watchCollection”,而不必使用多个手表,例如:

$watchCollection('[KeyPressed, Tags]', function (newValue) { }

newValue[0] is the value of KeyPressed, and newValue[1] is the value of Tags newValue [0]是KeyPressed的值,而newValue [1]是Tag的值

Or to go along with the accepted answer and minimize the amount of watches: 或者遵循公认的答案并尽量减少手表数量:

$scope.TruthyVal= function () {
    return $scope.KeyPressed && $scope.Tags !== '';
};

$scope.$watch('TruthyVal', function (newValue, oldValue) {
     if (newValue) {
         $scope.vm.someFunc= true;
     } else {
         $scope.vm.someFunc= false;
     }
}

In fact angular does not care what you write in html - function, variable or whatever. 实际上,angular并不关心您在html中编写的内容-函数,变量或其他内容。 It takes expression as string, parse it and calculate its value each digest cycle. 它以表达式为字符串,对其进行解析并在每个摘要循环中计算其值。 So, {{1 + 2}} and {{sum(1, 2)}} and {{1 | sum:2}} 因此, {{1 + 2}}{{sum(1, 2)}}{{1 | sum:2}} {{1 | sum:2}} are actually doing same job at more or less same speed. {{1 | sum:2}}实际上以差不多相同的速度完成相同的工作。

All three ways are legit and do not create memory leaks. 这三种方式都是合法的,不会造成内存泄漏。

The reason why it is always not recommended to use functions in ng-show is that lot of functions are time consuming, so your digest becomes very slow. 始终不建议在ng-show中使用功能的原因是许多功能很耗时,因此您的摘要会变得很慢。 And even if your functions are fast, you are not guaranteed that they wont grow up in future. 即使您的功能很快,也不能保证它们将来不会长大。

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

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