[英]ng-show with function not working
假定在函數返回1時顯示h2,但是即使返回1也不顯示標題。
以下角度代碼:
<md-virtual-repeat layout-wrap class="toast" ng-repeat="member in members| filter:searchText | orderBy:orderByFunction" >
<div class="subtitle" ng-show="showContrib(member) == 1" >
<h2> CONTRIBUTORS {{member.price}} -- {{member.amount}}</h2>
</div>
<div class="subtitle" ng-show="showTop(member) == 1" >
<h2> TOP CONTRIBUTORS {{Number(member.price)}} -- {{Number(member.amount)}}</h2>
</div>
<md-whiteframe class="md-whiteframe-z3 frieed" style="margin:6px; padding: 19px;" flex-sm="35" flex-gt-sm="25" flex-gt-md="20" layout layout-align="center center">
{{ member.amount}}
</md-whiteframe>
</md-virtual-repeat>
用這個js代碼:
$scope.showTop = function(member){
if($scope.topShow == 1){
return 0;
}
if(parseInt(member.price) < parseInt(member.amount)){
console.log('came here price');
$scope.topShow = 1;
return 1;
}
return 0;
};
$scope.showContrib = function(member){
$scope.conShow = 1;
// console.log('price='+member.price+"amount"+member.amount);
if($scope.conShow == 1){
return 0;
}
if(parseInt(member.price) == parseInt(member.amount)){
$scope.conShow = 1;
return 1;
}
return 0;
};
即使我在console.log中看到topShow和conShow都更改為1,它也總是評估為false。
在我看來就像您將conShow
設置為1,然后return 0
。 看這里:
$scope.conShow = 1;
if($scope.conShow == 1){
return 0;
}
因此,它實際上不是在檢查您以后的邏輯,而是返回除0以外的任何值。
這里有兩個問題。
正如@LokiSinclair指出的那樣, $scope.showContrib
將始終返回0。
其次,是在ng-show
使用功能。 當您使用函數確定ng-show
的值時,此函數將一遍又一遍地重新評估。 在$scope.showTop
函數中,將$scope.showTop
的值更改為1並返回1,但是由於您更改了范圍對象的值,因此可能要$scope.topShow
毫秒的時間,直到再次調用此函數為止,只有這樣時間它將返回0。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.