0

Quite new to angular JS and need some help. How do I use ng-if to display or hide different input fields? I'm currently using ng-show but it doesn't completely remove the DOM, therefore making it difficult during validation. I want the input fields displaying within a specific div to become mandatory ONLY when selected.

When I click Select Fund, I want the showme2 div to display and the fields to become mandatory. When I click Select Product, I want the showme1 div to display and the fields to become mandatory. See my current code below:

<div ng-show="showme1">
         <div class="form-group">
         <h3 class="col-xs-12 col-sm-3">Add Product details</h3>
           <label class="col-xs-12 col-sm-3 control-label" for="Product1</label>
           <div class="col-xs-12 col-sm-6">
              <input type="text" name="productName" class="form-control" id="productName1" required="required" placeholder="Product 1">
           </div>
        </div>

        <<div class="form-group">
         <h3 class="col-xs-12 col-sm-3">Add Product details</h3>
           <label class="col-xs-12 col-sm-3 control-label" for="Product2</label>
           <div class="col-xs-12 col-sm-6">
              <input type="text" name="productName" class="form-control" id="productName2" required="required" placeholder="Product 2">
           </div>
        </div>

        <div class="form-group">
           <label class="col-xs-12 col-sm-3 control-label"></label>
           <div class="col-xs-12 col-sm-6" align="center" ng-click="showme2=true; showme1=false"><a>(or Select Fund)</a><br /></div>
        </div>
</div>


<div ng-show="showme2">
         <div class="form-group">
         <h3 class="col-xs-12 col-sm-3">Add Fund details</h3>
           <label class="col-xs-12 col-sm-3 control-label" for="Product1</label>
           <div class="col-xs-12 col-sm-6">
              <input type="text" name="fundName" class="form-control" id="fundName1" required="required" placeholder="Fund 1">
           </div>
        </div>

        <<div class="form-group">
         <h3 class="col-xs-12 col-sm-3">Add Product details</h3>
           <label class="col-xs-12 col-sm-3 control-label" for="Product2</label>
           <div class="col-xs-12 col-sm-6">
              <input type="text" name="fundName" class="form-control" id="fundName2" required="required" placeholder="Fund 2">
           </div>
        </div>

            <div class="form-group">
           <label class="col-xs-12 col-sm-3 control-label"></label>
           <div class="col-xs-12 col-sm-6" ng-click="showme1=true; showme2=false" align="center"><a>(or Select Product)</a></div>
        </div>
</div>
Kasiriveni
  • 5,671
  • 1
  • 22
  • 31
EF_1000
  • 45
  • 1
  • 6
  • So what happens when you change `ng-show` to `ng-if`? They both expect a boolean expression – charlietfl Jun 04 '17 at 15:13
  • @charlietfl Yes. How do I make ng-if work? Cheers. – EF_1000 Jun 04 '17 at 15:55
  • That doesn't even answer the question asked. Need to be a lot more detailed if you want help here. Take some time to read [ask] and [mcve] – charlietfl Jun 04 '17 at 16:04
  • If it worked with ng-show before, it should "just work" if you replace the ng-show with ng-if. They both will display the element if the expression evaluates to true, the difference being ng-if removes it from the dom when it is not being displayed – chiliNUT Jun 04 '17 at 23:29
  • @charlietfl thanks for the feedback. Cheers – EF_1000 Jun 05 '17 at 02:05

1 Answers1

2

Reference this stackoverflow answer to understand how ng-if is used.

You can use ng-if to achieve if(){..} else{..} in Angular.js.

<div ng-if="data.id == 5">
<!-- If block -->
</div>
<div ng-if="data.id != 5">
<!-- Your Else Block -->
</div>

Additionally, I've modified your code snippet in a CodePen to use ng-if, see here. https://codepen.io/silicaRich/pen/PjwwPv

JS

var TestApp = angular.module("TestApp", []);
 

HTML

  <html>
  <head>
  </head>
  <body ng-app='TestApp'>

    <!-- Will display if showme == true -->
    <div ng-show="showme">
         <div class="form-group">
            <h3 class="col-xs-12 col-sm-3">Add Product details // showme1</h3>
           <label class="col-xs-12 col-sm-3 control-label" for="Product1"></label>
           <div class="col-xs-12 col-sm-6">
              <input type="text" name="productName" class="form-control" id="productName1" required="required" placeholder="Product 1">
           </div>
        </div>

        <div class="form-group">
         <h3 class="col-xs-12 col-sm-3">Add Product details // showme1</h3>
           <label class="col-xs-12 col-sm-3 control-label" for="Product2"></label>
           <div class="col-xs-12 col-sm-6">
              <input type="text" name="productName" class="form-control" id="productName2" required="required" placeholder="Product 2">
           </div>
        </div>

        <div class="form-group">
           <label class="col-xs-12 col-sm-3 control-label"></label>
           <div class="col-xs-12 col-sm-6" align="center" ng-click="showme=false;"><a>(or Select Fund) // showme2</a><br /></div>
        </div>
    </div>
    
    <!-- Will display if showme == false -->
    <div ng-show="!showme">
         <div class="form-group">
         <h3 class="col-xs-12 col-sm-3">Add Fund details // showme2 </h3>
           <label class="col-xs-12 col-sm-3 control-label" for="Product1"></label>
           <div class="col-xs-12 col-sm-6">
              <input type="text" name="fundName" class="form-control" id="fundName1" required="required" placeholder="Fund 1">
           </div>
        </div>

        <div class="form-group">
         <h3 class="col-xs-12 col-sm-3">Add Fund details // showme2 </h3>
           <label class="col-xs-12 col-sm-3 control-label" for="Product2"></label>
           <div class="col-xs-12 col-sm-6">
              <input type="text" name="fundName" class="form-control" id="fundName2" required="required" placeholder="Fund 2">
           </div>
        </div>

          <div class="form-group">
           <label class="col-xs-12 col-sm-3 control-label"></label>
           <div class="col-xs-12 col-sm-6" ng-click="showme=true;" align="center"><a>(or Select Product) // showme1</a></div>
        </div>
   </div>
    
  </body>
  </html>

Hope this helps.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Nicole Flokos
  • 77
  • 1
  • 9