【问题标题】:How to show or hide a div in Angular Js如何在 Angular Js 中显示或隐藏 div
【发布时间】:2015-10-01 09:44:01
【问题描述】:

我已经实现了两个 div,我在名为 div1 的 div 中使用了一些下拉控件。我想隐藏 div2,直到在下拉列表中选择任何值。我想知道如何隐藏或显示 div ng-change 或直到选择任何值 div2 不得显示。 代码:-

<div data-ng-app="CustomerNew" data-ng-controller="CreateCustomerCtrl as custom" ng-init="getFormData();">
    <div id="div1">
    <tr>
    <td nowrap>Company Name:
    </td>
    <td>
    <asp:TextBox ID="txtCompanyName" runat="server" CssClass="NormalTextBox" TabIndex="1" Width="160px" Height="10px" ng-model="custom.txtCompanyName" required=""></asp:TextBox>  
    </td>
    </tr>
    <tr>
    <td>Country:</td>
    <td>
    <select id="listHomeCountry1" style="width: 182px !important; height: 34px;">
    <option value="0">--- Select an option ---</option>
    <option data-ng-repeat="Cntry in listHomeCountry" ng-model="custom.listHomeCountry" ng-change="" value="{{Cntry._key}}">{{Cntry._value}}</option>
    </select>
    </tr> 


    <div id="div2">
     <table style="position: relative; left: 0px;">
    <tr align="left">
    <td nowrap style="width: 200px">
    <asp:Label ID="lblContactAddress1" runat="server" CssClass="NormalTextBox" Width="60%"></asp:Label></td>
    <td nowrap>
    <asp:TextBox ID="txtContactAddress1" TabIndex="3" Name="txtContactAddress1" runat="server" CssClass="NormalTextBox" Columns="35" Width="160px" Height="10px" ng-model="custom.txtContactAddress1" required=""></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    <asp:Label ID="lblContactAddress2" runat="server" CssClass="NormalTextBox"></asp:Label></td>
    <td>
    <asp:TextBox ID="txtContactAddress2" Name="txtContactAddress2" TabIndex="4" runat="server" CssClass="NormalTextBox" Columns="35" Width="160px" Height="10px" ng-model="custom.txtContactAddress2" required=""></asp:TextBox>
    </td>
    </tr>
    </div>
    </div>

现在我想要直到,除非我的下拉菜单选择了一个值,否则 div2 将被隐藏。

【问题讨论】:

标签: angularjs typescript typescript1.4 ng-hide typescript1.5


【解决方案1】:

首先为您的选择列表分配一个变量。例如

<select id="listHomeCountry1" ng-model="homeCounrty">

然后只需使用ng-show (doc)

<div id="div2" ng-show="homeCountry">

上面也等价于:

<div id="div2" ng-show="homeCountry != null">

【讨论】:

  • hmecountry 是我的 div id 还是什么?因为我必须隐藏完整的 div2
  • ng-model="homeCountry" 将简单地创建一个名为 homeCountry 的变量并将其绑定到 SELECT 的值。阅读this 了解更多信息。
猜你喜欢
  • 2020-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-23
  • 1970-01-01
相关资源
最近更新 更多