在这种情况下更好的ng-hide / show或ng-if?

时间:2015-01-28 00:54:17

标签: angularjs ionic-framework

我在我的应用程序中使用了Angular和Ionic,我在ng-show遇到了一些问题所以我决定改变并放置ng-if,现在我的应用程序正在按我的意愿运行,但是我有些疑惑。性能怎么样?并且正确地说我正在做什么?

这就是我现在的代码:

        <div ng-if="betSlip.length >= 1">
          <div class="list betslip-item">
            <div class="item item-divider betslip-header">
              APPLY TO ALL
            </div>

这就是我的方式:

        <div ng-show="betSlip.length >= 1">
          <div class="list betslip-item">
            <div class="item item-divider betslip-header">
              APPLY TO ALL
            </div>

更新

ng-switch怎么样?

1 个答案:

答案 0 :(得分:2)

ng-show / hide只是将display:none样式应用于你的组件,但是DOM元素仍然存在,并且仍在那里,另一方面,ng-if仅在条件满足时创建DOM元素,更好的ng-if或显示依赖于某些因素,条件切换的频率/速度是多少,这是基于用户交互还是基于快速变化的计算计算?还有,你的开关元素有多少个绑定? / p>

这是我何时使用其他

的标准
1 if the container is simple(none, or few bindings) and my dom is not too crowded already and doesn't have required fields or is not contained inside a form  i am ok using hide/show 
2 if my element contains a lot of bindings, my DOM is aleady extense, or the existance of the the content affects some how the result of the application ie(required fields or submit buttons inside a form) then i use ng-if or ng-switch

那些没有记录,但是我选择处理dom插入/显示

相关问题