2015-11-06 51 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