我们可以在一个div标签中使用ng-show和ng-if吗?

时间:2017-01-24 06:12:32

标签: angularjs ng-show angularjs-ng-if

以下代码段不起作用 请建议任何其他方式

<html>
`<div id="tablediv" ng-model="ngtable">
<div ng-show="ngtable">
<div ng-if="currentdevicetype == 'condition1'">
<!-- Other code to display contents -->
</div>`
</div>
</div>
</html> 

3 个答案:

答案 0 :(得分:0)

是的,你可以,两者都不同。

ng-show
当表达式求值为display:none时,

设置元素的false,而当表达式求值为ng-if时,false从DOM中删除元素

答案 1 :(得分:0)

查看此问题,了解ng-show和ng-if&amp;在哪里以及如何使用它们:

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

答案 2 :(得分:0)

在你的html代码中,你使用的是错误的,因为你正在使用ng-model作为div。

  <html>
    <div id="tablediv" ng-model="ngtable">
    <div ng-show="ngtable">
    <div ng-if="currentdevicetype == 'condition1'">
    <!-- Other code to display contents -->
    </div>
    </div>
    </div>
 </html>

ng-model用于绑定任何inputbox / textarea / select like标签的值,你不能绑定任何这样的值:

<div id="tablediv" ng-model="ngtable">

如果您删除此ng-model,那么您的代码将是这样的:

        <html>
            <div id="tablediv">
            <div ng-show="ngtable">
            <div ng-if="currentdevicetype == 'condition1'">
            <!-- Other code to display contents -->
            </div>
            </div>
            </div>
       </html>

现在,如果ngtable有一些值,则表示ng-show=true,然后

 <div ng-show=true>
            // all the elements are visible on the DOM.
 </div>

但是,如果ngtable没有任何值,则表示ng-show=false,然后:

<div ng-show=false>
                // all the elements are not visible on the DOM.
     </div>

在此代码中:

<div ng-if="currentdevicetype == 'condition1'">
        <!-- Other code to display contents -->
</div>

如果ng-if="currentdevicetype == 'condition1'"返回true,那么将创建所有元素,否则将不会创建元素。

相关问题