[英]ng-show/ng-hide limit multiple calls
I am trying to limit the call of ng-hide/ng-show. 我试图限制ng-hide / ng-show的通话。 Currently, what it does is it calls the getLicense function multiple times that will overloads the browser. 当前,它的作用是多次调用getLicense函数,这将使浏览器过载。
$scope.getLicense = function( value ) {
if( $sessionStorage.license === '' ) {
DashboardService.getLicense( ).then( function( data ) {
$scope.licenses = data;
var arr = [ ],
id = '';
for( var i in $scope.licenses ) {
arr.push( [ i ] );
}
$sessionStorage.license = arr;
} );
for( var cnt = 0; cnt < $sessionStorage.license.length; cnt++ ) {
if( $sessionStorage.license[ cnt ] == value ) {
console.log( 'true' );
return true;
break;
} else {
return false;
break;
}
}
} else {
for( var cnt = 0; cnt < $sessionStorage.license.length; cnt++ ) {
if( $sessionStorage.license[ cnt ] == value ) {
console.log('true');
return true;
break;
} else {
console.log('false');
return false;
break;
}
}
}
};
My HTML code looks like this: 我的HTML代码如下所示:
<md-list-item class="md-caption" ng-class="{'active': $state.includes('security.webcontrol')}" translate="SIDEBAR.NAV.WEBCONTROL.TITLE" ng-hide="getLicense('web_control_da')">
Giving a function to ng-show
/ hide
/ if
/ etc is a very bad practice . 给ng-show
/ hide
/ if
/ etc提供功能是非常糟糕的做法 。
Each time $digest
is called ( very often ) it check each watcher to see if it has changed. 每次( 非常频繁地 )调用$digest
它都会检查每个观察者以查看其是否已更改。 So it will have to execute your function to know if the result is different (or not). 因此,它必须执行您的函数才能知道结果是否不同(或不同)。
Add a console.log('function executed')
in your function getLicense
and you will see how often it is called. 在您的函数getLicense
添加console.log('function executed')
,您将看到它被调用的频率。
To avoid that (like Icycool explained) you have to replace that by a boolean in your scope. 为了避免这种情况(如Icycool所述),您必须在范围内将其替换为布尔值。 And only change the boolean when getLicense
should be tested. 并且只有在应该测试getLicense
时才更改布尔值。
For example : If getLicense
need to be calculated each time $sessionStorage.license
change (for example) : 例如:如果每次$sessionStorage.license
更改时都需要计算getLicense
(例如):
$scope.licence = getLicense();
$scope.watch("$sessionStorage.license", function (newValue, oldValue){
$scope.licence = getLicense();
});
And in your view/template : ng-hide="licence"
在您的视图/模板中: ng-hide="licence"
So it will execute your getLicense
only when it does really matter. 因此,仅当确实重要时,它才会执行getLicense
。
You can assign it to a scope variable and have ng-hide
point to that instead. 您可以将其分配给作用域变量,并使其具有ng-hide
指向。 Call check license on other occasions. 在其他情况下致电支票许可证。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.