2014-06-28 59 views
0

我遇到了ng級的問題。我想改變布爾值來控制ng-class,但是這個值被設置爲異步模式並且視圖文件剛剛被加載。沒有點擊的角度ng級更改

這是html的觀點

<li ng-class="{active: isPageView('media')}"> 
       <div class="pointer" ng-show="isPageView('media')"> 
        <div class="arrow"></div> 
        <div class="arrow_border"></div> 
       </div> 
       <a ng-class="{true: 'dropdown-toggle', false: ''}[disabled]" href=""> 
        <i class="icon-camera-retro"></i> 
        <span>Media</span> 
        <i class="icon-chevron-down"></i> 
       </a> 
       <ul class="submenu"> 
        ........... 
       </ul> 
      </li> 

,這是控制器的一部分

$scope.init = function(){ 
     _.mixin(_.str.exports()); 
     $scope.disabled = true; 
     $rootScope.emptyCompose(); 
     var localstorage = localStorageService.get('Userprofiles'); 
     var allExpired = true; 
     if (localstorage) { 
      $rootScope.userprofiles = localstorage; 
      _.each($rootScope.userprofiles, function(userprofile){ 
       if(moment().isBefore(moment.unix(userprofile.expireTime))){ 
        allExpired = false; 
       } 
      }); 
      if(allExpired){ 
       $scope.disabled = ! $scope.disabled; 
       $rootScope.modalInstance = $modal.open({ 
        templateUrl: 'views/composebilling.html', 
        controller: 'BillingCtrl', 
        keyboard: false, 
        backdrop: 'static' 
       }); 
      } 
     }else { 
      Index.query(function(userprofiles) { 
       $rootScope.userprofiles = userprofiles; 
       localStorageService.add('Userprofiles', userprofiles); 
       _.each($rootScope.userprofiles, function(userprofile){ 
        if(moment().isBefore(moment.unix(userprofile.expireTime))){ 
         allExpired = false; 
        } 
       }); 
       if(allExpired){ 
        $scope.disabled = ! $scope.disabled; 
        $rootScope.modalInstance = $modal.open({ 
         templateUrl: 'views/composebilling.html', 
         controller: 'BillingCtrl', 
         keyboard: false, 
         backdrop: 'static' 
        }); 
       } 
      }); 
     } 

如果$ scope.disabled過程中不會對服務器查詢設置好的所有火好,否則我有麻煩

回答

1

您正在使用ng-classisPageView('media')但該功能是不是你提供的代碼樣本。如果有什麼依賴於$scope.disabled我沒有看到它。此外,該ng-class說法不正確地寫入:

<a ng-class="{true: 'dropdown-toggle', false: ''}[disabled]" href=""> 

應該寫這樣的事:

<a ng-class="{'dropdown-toggle': true-condition, 'disabled': false-condition}" href=""> 

...其中true-condition是一些表達式,將評估爲TRUEfalse-condition是一些表達式將評估爲FALSE。總是將這些設置爲TRUEFALSE是無用的,因爲您可以在沒有ng-class的情況下執行此操作。有一個名爲truefalse的CSS類只是不好的代碼。