Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs ngHide delayed with ngShow

Tags:

angularjs

Good morning, i have a field, when it is viewed it is a label, when it is modified it is an input field, i trigger this with ng-show and ng-hide and a button that enables a boolean value. When i activate the editing mode the label hides and the input field shows, it is in realtime, when i click cancel and switch the boolean, the label appears but the input field takes some time to hide, so i have a very bad visual effect. here some code

<input ng-model="name" ng-show="editing">
<label ng-hide="editing">{{name}}</label>

<button ng-click="editing=true">Edit</button>
<button ng-click="editing=false">Cancel</button>

is there a way to fix this issue?

Thank you

like image 627
CaTourist Avatar asked Jan 23 '15 22:01

CaTourist


People also ask

What is the equivalent of ngShow and ngHide in angular?

ngShow and ngHide are two ng directives in AngularJS used to show and hide the elements respectively.

Can we use ng-show and Ng-hide together?

Absolutely not. First of all, the two directives can trip over each other( see this JSFiddle, as provided by Joel Skrepnek), and is generally just bad design.

Can we use NGIF and Ng-show together?

ng-if is better in this regard. Using it in place of ng-show will prevent the heavy content from being rendered in the first place if the expression is false. However, its strength is also its weakness, because if the user hides the chart and then shows it again, the content is rendered from scratch each time.

What is the difference between the directives ng-show and Ng-if?

ng-if can only render data whenever the condition is true. It doesn't have any rendered data until the condition is true. ng-show can show and hide the rendered data, that is, it always kept the rendered data and show or hide on the basis of that directives.


2 Answers

do you have ngAnimate included?

if so, this can happen. At our projects we declare and use a class on every dom element where we want to animate, and we tell angular to animate just those elements in the config callback:

$animateProvider.classNameFilter(/animate/);
like image 162
Ins Avatar answered Oct 16 '22 20:10

Ins


I had the same problem all the time I had to elements with the same var to hide/show (because ng-animate) , this happens with ng-show / ng-hide / ng-if and maybe also with ng-switch but not with ng-class. So a quick fix is change your code for:

<input ng-model="name" ng-class="{'ng-hide':!editing}">
<label ng-class="{'ng-hide':editing}">{{name}}</label>

<button ng-click="editing=true">Edit</button>
<button ng-click="editing=false">Cancel</button>
like image 36
Octavioamu Avatar answered Oct 16 '22 20:10

Octavioamu