如何仅在一个元素上激活ng-class?

时间:2018-07-05 14:41:25

标签: javascript angularjs

每个元素都有ng-repeatng-class。我用它来选择元素(为选定的元素添加边框)。

<div class="main-block_channel_create">
<section class="parent_messageList cancelDelete">
    <div id="section_animate" class="messagesList_block cancelDelete" infinite-scroll='messagelist.nextSet()' infinite-scroll-listen-for-event='anEvent' infinite-scroll-distance='100'>
        <!-- User Images List -->
        <a href="" class="messageBl message_preview animated" ng-repeat='preview in messagelist.previewList'>
            <div class="image_container_preview" ng-class="{community_select: item.isSelected}" ng-click='communitySelect(preview)' attr="{{preview.preview_id}}">
                <img class="deleteMessageBtn" src="images/close_icon.svg" ng-click="deleteMessage(preview.message_id)">
                <div class="spinner_container">
                    <!--<img src="/images/logo-80.svg" class='spinner_img'>-->
                    <img class="spinner_logo_vertical" src="images/logo_vertical-part.svg" alt="">
                    <img class="spinner_logo_left" src="images/logo_left-part.svg" alt="">
                    <img class="spinner_logo_right" src="images/logo_right-part.svg" alt="">
                </div>
                <img class="message_preview-image" src="{{preview.image}}" alt="">
                <!--  Tсли нет изображения — показываем текст -->
                <div class="message_preview-text MediumNormalJunior" ng-if="!preview.image">
                    <div class="message_preview-text-inner" ng-if='preview.name'>
                        {{preview.name}}
                    </div>
                    <!-- если нету и текста показываем empty-->
                </div>
                <div class="empty_message" ng-if='!preview.text && !preview.image'>
                     <!--<h4>Empty</h4> -->
                </div>
            </div>
            <div class="stats" ng-show='preview.total_score > 0'>
                <p>{{preview.total_score}}</p>
            </div>
        </a>
        <footer class="listFooter">

        </footer>
    </div>
</section>

无礼

.community_select
    border: 3px solid white

指令

(function(){
    'use strict';

angular
    .module('buzz')
    .directive('channelcreate', channelcreate);

function channelcreate($rootScope, $location, $timeout, $cookies, $window, communityApiService, getCommunities){

    return{
        restrict: "E",
        replace: true,
        scope: true,
        templateUrl: '/templates/channelCreate/channelCreate.html',
        link: function($scope){
            // $rootScope.showChannelCreate = null;

            // Select communities for create new channel
            $scope.communityList = [];
            $scope.communitySelect = function(communityId){
                $scope.selected = false;
                if ($scope.communityList.indexOf(communityId) == -1){
                    $scope.communityList.push(communityId);

                } else {
                    $scope.communityList.pop(communityId)
                }

                console.log($scope.communityList);
            };

            // all messages preview are loaded from messagesLoadFactory
            $scope.messagelist = new getCommunities();

        }
    };
};

})();

单击id可以识别每个div。我如何只更改一个元素,但不能全部更改?

2 个答案:

答案 0 :(得分:1)

登录后...一个选项可以是用communityList.indexOf(preview.id) != -1检查元素的ID是否在所选元素的列表中,以便您的ng-class看起来像:

ng-class="{community_select: communityList.indexOf(preview.id) != -1}"

编辑

此外,从id删除$scope.communityList时,请确保先找到其索引,然后再使用splice删除它。

现在,用于删除/添加id的部分看起来像这样:

// ... content omitted
$scope.communitySelect = function(communityId) {
  $scope.selected = false;
  var index = $scope.communityList.indexOf(communityId);
  if (index == -1) {
    $scope.communityList.push(communityId);
  } else {
    $scope.communityList.splice(index, 1)
    //                          ^^^    ^
    //        remove    starting_here  one_element
  }

  console.log($scope.communityList);
};
// ... content omitted

答案 1 :(得分:1)

我猜想您要在屏幕上保持选中状态以突出显示,因为我不这样认为,因此您需要维护额外的收藏夹列表。相反,您可以在该记录上添加标志isSelected并根据用户的点击对其进行切换。

HTML

每个元素都有ng-repeat和ng-class。我用它来选择元素(为选定的元素添加边框)。

<a href="" class="messageBl message_preview animated" 
  ng-repeat='preview in messagelist.previewList'>
    <div class="image_container_preview" 
     ng-class="{community_select: item.isSelected}" 
     ng-click='communitySelect(preview)' 
     attr="{{preview.preview_id}}">
</a>

代码

$scope.communitySelect = function(communityId) {
  item.isSelected = !item.isSelected;
};

每当需要选定预览列表时,都可以轻松地遍历previews集合并抓住那些带有isSelected标志被选中的对象。

var selected = $scope.previews.map(i => i.isSelected);