1

I am trying to get data in angular js controller using angularjs form submission ng-submit="method"

Normally all data I can see in controller using alert(). Now the problem is if I use ng-if condition inside of angular form then this specific field value show undefined. How can I solve this?

<div class="row" id="BarcodeDiv">

    <span ng-model="br" ng-init="br=1" id="br" style="display:none;"><i class="fa fa-etsy" aria-hidden="true"></i></span>

    <span ng-if="br==2">
        <div class="col-sm-6">
            <label>Serial Number</label>  <span ng-click="barCodeChanger()"  style="cursor:pointer;"><i class="icon-rotate-cw3"></i></i></span>
    <input type="text" name="Barcode" ng-model="Barcode1" class="form-control" placeholder="123">
</div>
<div class="col-sm-3">
    <label>From</label>
    <input type="text" name="StartFrom" ng-model="StartFrom" id="StartFrom" class="form-control" placeholder="4" style="padding:5px;">
</div>
<div class="col-sm-3">
    <label>To</label>
    <input type="text" name="ToEnd" ng-model="ToEnd" id="ToEnd" class="form-control" placeholder="9" style="padding:5px;">
</div>
</span>
<span ng-if="br==1">
            <div class="col-sm-12">
                <label>Serial Number</label> <span ng-click="barCodeChanger()" style="cursor:pointer;"><i class="icon-rotate-ccw3"></i></span>
<input type="text" name="Barcode" ng-model="Barcode" class="form-control" placeholder="123">
</div>
</span>

</div>
Vivz
  • 6,625
  • 2
  • 17
  • 33
Rakib Ahmed
  • 31
  • 1
  • 5
  • 2
    Don't bind to attributes of the scope. Bind to attributes of an object which is on the scope. ng-if defines its own scope. Also, don't use ng-init. Initialize values from the controller code. – JB Nizet Jul 30 '17 at 19:04

1 Answers1

0

Keep in mind that ng-if is scope true so the vars won't necessarily show in your controller. Try replacing ng-if with ng-show. You should see the values.

However, ng-if is more efficient than ng-show.
Another option would be to create object in your controller let's say $scope.submitData = {} and bind Barcode1, ... via this object. So in your controller you would have to initialize `$scope.submitData = {}' and in the view you would reference it as

ng-model='submitData.Button1

Jarek Kulikowski
  • 1,399
  • 8
  • 9