ng-show从未显示过

时间:2016-01-12 06:01:48

标签: javascript angularjs

以下代码始终将Top贡献者评为true asn Contributors为false。我可以看到member.price和member.amount有变化吗?任何人都有一个想法,我做错了什么?

<md-virtual-repeat layout-wrap class="toast" ng-repeat="member in members| filter:searchText | orderBy:orderByFunction" >
    <div class="subtitle"  ng-show="parseInt(member.price) <= parseInt(member.amount)" >
        <h2> CONTRIBUTORS {{member.price}} -- {{member.amount}}</h2>
    </div>
    <div class="subtitle"  ng-hide="parseInt(member.amount) <= parseInt(member.price)" >
        <h2> TOP CONTRIBUTORS  {{member.price}} -- {{member.amount}}</h2>
    </div>

所以我开始这样:

  <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-hide="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;

};

然后我切换到第一部分尝试进行故障排除。

3 个答案:

答案 0 :(得分:4)

你不能使用parseInt,而是我想用* 1来使它成为int。

<div class="subtitle"  ng-show="member.price*1 <= member.amount*1" >
    <h2> CONTRIBUTORS {{member.price}} -- {{member.amount}}</h2>
</div>

答案 1 :(得分:2)

parseInt在视图中无法工作。

视图仅适用于范围的属性。

在作用域上创建一个比较器方法,然后从视图中调用它。

试试这个

$scope.isGreater=function(a,b){
  return parseInt(a) <= parseInt(b);
}

视图

<div class="subtitle"  ng-show="isGreater(member.price,member.amount)" >
    <h2> CONTRIBUTORS {{member.price}} -- {{member.amount}}</h2>
</div>
<div class="subtitle"  ng-hide="isGreater(member.amount, member.price)" >
    <h2> TOP CONTRIBUTORS  {{member.price}} -- {{member.amount}}</h2>
</div>

答案 2 :(得分:0)

您不能像模板中那样parseInt

只能在控制器模板中使用属于控制器范围的元素(函数,变量)。

要在模板中使用window,请将parseInt对象的$scope.parseInt = window.parseInt; 指定给控制器的范围:

//You should try 

edittext..addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });