简体   繁体   中英

Ng-hide and last:child CSS

I have;

<div class="exampleDiv">
    <element1></element1>
    <element2></element2>
    <element3></element3>
</div>

then some CSS using the last:child selector (only margin-right:0 FYI). However I use a conditional ng-hide on elements 1-3 and so if element3 is hidden, the last:child CSS is not moved to element2 as technically it is rendered in the DOM then hidden.

So I was wondering is there a way to use ng-hide and have something like a 'last-visible-element' selector. So that as ng-hide does its work, the last element the user can see has this CSS applied to it?

Thanks for any help in advance!

is remove() what you look for since it does remove element from the dom tree:

 var myApp = angular.module("myApp", []); myApp.controller("myController", function($scope) { $scope.removeElement = function() { var elmn = angular.element( document.querySelector( '.test :last-child' ) ); elmn.remove(); }; }); 
 .test { display: flex; justify-content: space-between; } .test div { border: solid; min-width: 5em; } .test :last-child { color: red; } .test :last-child:before { content:"actual last-child "; color:green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div ng-app="myApp"> <p>each time you click, last element is removed</p> <div ng-controller="myController" class="test"> <input type="button" value="Remove Div " ng-click="removeElement()"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>last-child</div> </div> </div> 

http://codepen.io/gc-nomade/pen/KmvKdV

you may try adding dynamic class based on some condition , something like :-

css / style

.show {
    display: inline;
}
.hide {
    display: none;
}

script

<div class="exampleDiv">
   <element1 ng-class="{'show' : item.isVisible == true, 'hide' : item.isVisible == false}"></element1>
   <element2 ng-class="{'show' : item.isVisible == true, 'hide' : item.isVisible == false}"></element2>
   <element3 ng-class="{'show' : item.isVisible == true, 'hide' : item.isVisible == false}"></element3>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM