Add class to an element in Angular 4

前端 未结 5 2062
北荒
北荒 2021-02-01 14:28

I was trying to create an image gallery with Angular 4.The logic behind this is to add a Cascading Style Sheet (CSS) class to the selected image that will show a red border on t

5条回答
  •  灰色年华
    2021-02-01 15:00

    you can try this without any java script you can do that just by using CSS

    img:active,
    img:focus,
    img:hover{ 
    border: 10px solid red !important
    }
    

    of if your case is to add any other css class by clicking you can use query selector like

    
    
    
    
    

    in controller first search for any image with red border and remove it then by passing the image id add the border class to that image

    $scope.changeClass = function(id){
    angular.element(document.querySelector('.some-class').removeClass('.some-class');
    angular.element(document.querySelector(id)).addClass('.some-class');
    }
    

提交回复
热议问题