Angular ng-if and ng-show combination

后端 未结 4 577
不知归路
不知归路 2020-12-31 06:18

Imagine some heavy content that might be rendered on a web page, such as a chart. Angular gives 2 options to toggle the visibility of said content.

ng-show

4条回答
  •  情话喂你
    2020-12-31 07:17

    +1 on Denis's answer, but just for completeness-sake, it can even be simplified further by keeping the logic in the View without "polluting" the controller:

    
    
    Heavy content

    plunker

    EDIT: the version above could be further improved (and simplified) with one-time binding to reduce an unnecessary $watch on once - this will only work in Angular 1.3+:

    Heavy content

    The undefined is needed to ensure that the watched value does not "stabilize" before it becomes true. Once it stabilizes, it also loses the $watch, so it would not be impacted by any further change to show.

提交回复
热议问题