2013-06-11 12 views
9

Sono un newb di angular.js e ho ereditato il codice. Esiste un set di dati (customerType) che viene visualizzato utilizzando ng-repeat. Ci sono 3 righe di tabella che voglio visualizzare solo se customerType.type è uguale a "nuovo". Queste 3 righe hanno tutte "ng-hide =" nei rispettivi tag sottostanti. Come posso farlo in angular.js? Le direttive che vedo sembrano tutte applicabili all'intera schermata, quindi non sono sicuro di come modificare una variabile di ambito solo per una parte del display. Ecco il codice html:Modifica ng-hide in base al valore variabile in angular.js

<div ng-repeat="customerType in customerTypes" class="span6"> 
    <table class="table table-bordered table-striped"> 
    <thead> 
     <tr> 
     <th colspan="3"> 
      <div class="span11 centered-text"> 
       {{customerType.title}} 
      </div> 
      <div class="span1"> 
       <a href="javascript:void(0);" ng-click="pullDetail(customerType.type, customerType.clients)" title="Details"><i class="icon-list-alt"></i></a> 
      </div> 
     </th> 
     </tr> 
     <tr> 
     <th></th> 
     <th> 
      <div class="centered-text"> 
      Month Selected 
      </div> 
     </th> 
     <th> 
      <div class="centered-text"> 
      YTD 
      </div> 
     </th> 
     </tr>        
    </thead> 
    <tbody> 
     <tr> 
     <td>Revenue</td> 
     <td>{{customerType.month.revenue}}</td> 
     <td>{{customerType.ytd.revenue}}</td> 
     </tr> 
     <tr> 
     <td>Gross Profit</td> 
     <td>{{customerType.month.gross}}</td> 
     <td>{{customerType.ytd.gross}}</td> 
     </tr> 
     <tr> 
     <td># of Customers</td> 
     <td>{{customerType.month.customers}}</td> 
     <td>{{customerType.ytd.customers}}</td> 
     </tr> 
     <tr> 
     <td>Avg GP per Customer</td> 
     <td>{{customerType.month.avg_gp}}</td> 
     <td>{{customerType.ytd.avg_gp}}</td> 
     </tr> 
     <tr> 
     <td># of Jobs</td> 
     <td>{{customerType.month.jobs}}</td> 
     <td>{{customerType.ytd.jobs}}</td> 
     </tr> 
     <tr ng-hide=> 
     <td>First Time Client - Revenue</td> 
     <td>{{customerType.month.ftc_revenue}}</td> 
     <td>N/A</td> 
     </tr> 
     <tr ng-hide=> 
     <td>First Time Client - Gross Profit</td> 
     <td>{{customerType.month.ftc_gross}}</td> 
     <td>N/A</td> 
     </tr> 
     <tr ng-hide=> 
     <td>First Time Client - # of Customers</td> 
     <td>{{customerType.month.ftc_customers}}</td> 
     <td>-----</td> 
     </tr> 
     <tr ng-hide=> 
     <td>First Time Client - # of Jobs</td> 
     <td>{{customerType.month.ftc_jobs}}</td> 
     <td>-----</td> 
     </tr> 
     <tr> 
     <td>% on Contract</td> 
     <td>{{customerType.month.contract}}</td> 
     <td>{{customerType.ytd.contract}}</td> 
     </tr> 
    </tbody> 
    </table> 
</div> 

e qui ci sono le attuali direttive:

var ReportController = function($scope, $http){ 
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded"; 

$scope.filter = true; 
$scope.report = false; 
$scope.detail = false; 

$scope.customerTypes = []; 
$scope.detail_report = []; 
$scope.companies = companies; 
$scope.nsperiods = nsperiods; 
$scope.users = users; 

$scope.pullReport = function(sync){ 
    if(sync){ 
     var xsr = {request: 'report', company_no: $scope.company, nsperiod: $scope.nsperiod, users_no: $scope.user}; 
     $('form button').button('loading'); 
     $http({ 
      method: 'POST', 
      headers: {'Content-Type': 'application/x-www-form-urlencoded'}, 
      transformRequest: function(obj) { 
       var str = []; 
       for(var p in obj) 
       str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); 
       return str.join("&"); 
      }, 
      data: xsr 
     }).success(function(response){ 
      $scope.filter = false; 
      $scope.report = true; 
      $scope.detail = false; 
      $('form button').button('reset'); 
      $scope.customerTypes = response; 
     }).error(function(error){ 
      $('form button').button('reset'); 
      return; 
     }); 
    }else{ 
     $scope.filter = false; 
     $scope.report = true; 
     $scope.detail = false; 
    } 
} 

$scope.backToFilters = function(){ 
    $scope.filter = true; 
    $scope.report = false; 
    $scope.detail = false; 
} 

$scope.pullDetail = function(type, clients){ 
    var xsr = { 
     request: 'detail', 
     type: type, 
     company_no: $scope.company, 
     nsperiod: $scope.nsperiod, 
     users_no: $scope.user 
    }; 
    $http({ 
     method: 'POST', 
     headers: {'Content-Type': 'application/x-www-form-urlencoded'}, 
     transformRequest: function(obj) { 
      var str = []; 
      for(var p in obj) 
      str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); 
      return str.join("&"); 
     }, 
     data: xsr 
    }).success(function(response){ 
     $scope.filter = false; 
     $scope.report = false; 
     $scope.detail = true; 
     $scope.detail_report = response; 
    }).error(function(error){ 
     return; 
    });     
} 
}; 

Qualsiasi aiuto sarebbe molto apprezzato. Sto cercando di capovolgerlo, ma qualcosa non si sta proprio connettendo.

risposta

26

Mostrare/nascondere semplicemente in base a tale variabile?
Farei:

ng-show="customerType.type == 'new'" 
+1

Sì, ero MODO overthinking che. Grazie per aver assecondato la mia stupidità agreco. – tjfo

+2

per una buona misura e per evitare problemi, si consideri l'utilizzo di equazioni triple anziché doppie equivalenti. Cioè ng-show = "customerType.type === 'new'", perché con == tutto è convertito in tipo prima di valutare l'uguaglianza. In opposizione, con equazioni triple, l'uguaglianza viene testata così com'è, nulla viene convertito prima di essere valutato. Il primo può portare a risultati inaspettati. Ecco un bel grafico che illustra questo: http://dorey.github.io/JavaScript-Equality-Table/ –

Problemi correlati