2015-11-06 52 views
7

我正在開發一個需要允許用戶在圖像上添加/刪除標籤的項目。ng-class在值更改時沒有正確更新

有網格視圖,單視圖和混合視圖。 網格視圖在網格中顯示圖像大拇指, 單個視圖逐個顯示圖像 並且混合視圖在後臺具有網格,前面有單個圖像(放大功能)。

所有這些視圖都有一個包含可應用於圖像的標籤的頁腳。然而,電網有自己的頁腳,而單一和混合的意見分享他們的觀點。

下面是對那些在HTML端代碼:

<section id="noRefContainer" class="photosWrapper photosWrapper-cq" style="display: block"> <!--ng-controller="gridController">--> 
     <div class="images-cq__wrapper" ng-show="displayStyle.style == 'grid' || displayStyle.style == 'both'"> 
      <div class="images-cq__item" ng-repeat="photo in displayedPhotos"> 
       <div ng-class="{active: photo.selected}"> 
        <label for="{{photo.uuid}}"> 
         <div class="img-cq"> 
          <img ng-src="{{photo.thumbPath100}}" alt="Alternate Text" ng-click="selectionEvent({value: photo.uuid, event: $event, index: $index})" /> 
          <a href="#" class="zoom-cq" title="zoom" ng-click="zoomOpen({value: $index})">zoom</a> 
         </div> 
         <p> 
          {{photo.title}} 
         </p> 
        </label> 
       </div> 
      </div> 
      <div class="images-cq__footer open"> 
       <p> 
        <span>Tagger les</span> 
        <strong>{{selectedPhotos.length}}</strong> 
        <span>éléments sélectionnés</span> 
       </p> 
       <div class="images-cq__dropdown top"> 
        <a href="#">...</a> 
        <ul> 
         <li><a href="#" ng-click="selectAll()">Sélectionner toutes les images</a></li> 
         <li><a href="#" ng-click="deselectAll()">Désélectionner toutes les images</a></li> 
        </ul> 
       </div> 
       <div class="images-cq__tags"> 
        <ul> 
         <li ng-repeat="tag in tags"> 
          <a href="#" ng-class="{'active': tag.status == 'active', 'selected': tag.status == 'selected'}" ng-click="tagSelectionEvent({value : tag.value})">{{tag.name}}</a> 
         </li> 
        </ul> 
       </div> 
       <small>Attention, ceci effacera les précédents tags.</small> 
      </div> 
     </div> 
     <div ng-class="{'images-cq__lightbox': displayStyle.style == 'both', 'images-cq__wrapper': displayStyle.style == 'single', single: displayStyle.style == 'single'}" ng-show="displayStyle.style == 'both' || displayStyle.style == 'single'"> 
      <div class="images-cq__carousel"> 
       <a href="" class="images-cq__carouselclose" ng-click="zoomClose()" ng-show="displayStyle.style == 'both'"> 
        Close 
       </a> 
       <div class="images-cq__carouselcontent" id="carousel"> 
       </div> 
       <div class="images-cq__carouselfooter"> 
        <div class="images-cq__tags"> 
         <ul> 
          <li ng-repeat="tag in tags"> 
           <a href="#" ng-class="{'active': tag.status == 'active', 'selected': tag.status == 'selected'}" ng-click="zoomTagSelectionEvent({value : tag.value})">{{tag.name}}</a> 
          </li> 
         </ul> 
        </div> 
       </div> 
      </div> 
     </div> 
    </section> 

而app.js側代碼:

$scope.tags = []; 

$scope.tags = [{ name: 'CQ:OK', count: 0, status: '', value: 'CQ:OK' }, 
     { name: 'CQ:OK_NO_ZOOM', count: 0, status: '', value: 'CQ:OK_NO_ZOOM' }, 
     { name: 'CQ:KO', count: 0, status: '', value: 'CQ:KO' }, 
     { name: 'Chromie', count: 0, status: '', value: 'Chromie' }, 
     { name: 'Détourer', count: 0, status: '', value: 'Détourer' }, 
     { name: 'Nettoyer_redresser', count: 0, status: '', value: 'Nettoyer_redresser' }, 
     { name: 'Interne', count: 0, status: '', value: 'Interne' }, 
     { name: 'Otsc', count: 0, status: '', value: 'Otsc' }]; 

$scope.zoomTagSelectionEvent = function (tag) { 
     var photo = $scope.carousel.settings.images[$scope.carousel.settings.currentImage]; 
     if ($scope.hasTag(photo, tag.value)) { 
      $scope.removeTag(photo, tag.value); 
     } 
     else { 
      $scope.addTag(photo, tag.value); 
     } 
     $scope.updateTagStatus(tag.value); 
    } 

$scope.tagSelectionEvent = function (tag) { 
     if ($scope.allHaveTag($scope.selectedPhotos, tag.value)) { 
      $scope.allRemoveTag($scope.selectedPhotos, tag.value); 
     } 
     else { 
      $scope.allAddTag($scope.selectedPhotos, tag.value); 
     } 
     $scope.updateTagStatus(tag.value); 
    } 

$scope.updateAllTagStatus = function() { 
     angular.forEach($scope.tags, function (value, key) { 
      $scope.updateTagStatus(value.value); 
     }); 
    } 

     $scope.updateTagStatus = function (tag) { 
     var currentTag = $scope.getTag(tag); 

     if ($scope.displayStyle.style == 'grid') 
     { 
      var tagged = $scope.countTagged($scope.selectedPhotos, tag); 
      if (tagged == 0) { 
       currentTag.status = 'none'; 
      } 
      else if (tagged < $scope.selectedPhotos.length) { 
       currentTag.status = 'selected'; 
      } 
      else { 
       currentTag.status = 'active'; 
      } 
     } 
     else { 
      if ($scope.carousel.settings.currentImage !== false) 
      { 
       var photo = $scope.carousel.settings.images[$scope.carousel.settings.currentImage]; 
       var res = $scope.hasTag(photo, tag); 
       if (res) { 
        currentTag.status = 'active'; 
       } 
       else { 
        currentTag.status = 'none'; 
       } 
      } 
     } 
     console.log('tag ' + tag + ' status updated'); 
    } 

標籤被應用到圖像每一次,標籤狀態更新,它應該更新ng-class表達式結果。唯一得到正確更新的部分是網格頁腳。只有在顯示視圖時,它纔會在單個/混合視圖更新之間共享。

至於我試圖解決這個問題,我已經嘗試在每次調用標籤更新後使用$ scope.apply(),嘗試將其放在updateTagStatus函數的末尾。我也嘗試改變表達式(帶有/不帶引號的類名,將類設置爲標籤狀態......),所有這些都只適用於網格頁腳,而不適用於其他頁面。我還檢查了狀態是否正確更新,唯一的問題是更新顯示。

請幫忙。

更新:

我很抱歉,這裏沒有回答中,有一個巨大的變陣在很短的時間量的項目列表中,這樣導致此問題的代碼也沒有了,這也消除了問題共。我忙於工作,忘了更新這個項目。

但是,感謝您花時間來到這裏,並試圖幫助我。

我不知道在這種情況下我該怎麼做。

+5

會更容易幫助,如果你能在Plunker複製它。 – tasseKATT

+0

我們可以得到'getTag(tag_name)'方法嗎?它似乎從您的app.js中丟失,可能是解決方案的關鍵。 –

回答

0

它第一次看起來像var currentTag = $scope.getTag(tag);這條線是罪魁禍首。它可能不會返回適當的標籤angular object

或者您可以嘗試正確使用$ apply功能。 即用戶$apply像每次更新語句:

$scope.$apply(function() { currentTag.status = 'selected'; }); 

$scope.$apply(function() { currentTag.status = 'none'; }); 
+0

請參閱更新後的帖子。 – madks13