What is the difference between ng-if and ng-show/ng-hide

前端 未结 12 1391
执笔经年
执笔经年 2020-11-22 02:22

I\'m trying to understand the difference between ng-if and ng-show/ng-hide, but they look the same to me.

Is there a differenc

12条回答
  •  不知归路
    2020-11-22 03:08

    One important thing to note about ng-if and ng-show is that when using form controls it is better to use ng-if because it completely removes the element from the dom.

    This difference is important because if you create an input field with required="true" and then set ng-show="false" to hide it, Chrome will throw the following error when the user tries to submit the form:

    An invalid form control with name='' is not focusable.
    

    The reason being the input field is present and it is required but since it is hidden Chrome cannot focus on it. This can literally break your code as this error halts script execution. So be careful!

提交回复
热议问题