When to favor ng-if vs. ng-show/ng-hide?

前端 未结 7 1448
陌清茗
陌清茗 2020-11-22 08:39

I understand that ng-show and ng-hide affect the class set on an element and that ng-if controls whether an element is rendered as par

7条回答
  •  有刺的猬
    2020-11-22 09:06

    From my experience:

    1) If your page has a toggle that uses ng-if/ng-show to show/hide something, ng-if causes more of a browser delay (slower). For example: if you have a button used to toggle between two views, ng-show seems to be faster.

    2) ng-if will create/destroy scope when it evaluates to true/false. If you have a controller attached to the ng-if, that controller code will get executed every time the ng-if evaluates to true. If you are using ng-show, the controller code only gets executed once. So if you have a button that toggles between multiple views, using ng-if and ng-show would make a huge difference in how you write your controller code.

提交回复
热议问题