Search code examples
javascriptangularjsangularjs-scope

Angular losing scope in ng-form template


I am trying this ,

    <div ng-form="sadadPaymentForm" class="sadadPaymentForm"  ng-if="vm.isSadadEnabled" name="sadadPaymentForm"  validate-popup-form>
     <div ng-if="vm.isSadadEnabled">
       <div class="credit-debit-card-div" ng-include="'sadadTemplate'" ng-show="vm.view_tab == 'tab7'">
       </div>
    </div>
   </div>
   <span ng-show="vm.view_tab=='tab7' && vm.isSadadEnabled">
     <button type="button" class="primary inline pay-now" id="paynowbtn" ng-disabled="!vm.checked3 || vm.sadadPaymentForm.$invalid" ng-click="vm.payByVoucher();" analytics-on="click" analytics-event="uaevent" analytics-eventcategory="Payment" analytics-eventaction="PayNow"
             analytics-eventlabel="Pay now">
               Pay by sadad
     </button>
   </span>

And , my template in another html file

  <script type="text/ng-template" id="sadadTemplate">
    <input fieldlabel="Online Payment ID" type="text" name="onlinePaymentId" ng-model="vm.sadadpayment.onlinePaymentId"  class="form-control input-type-text"
                    ng-model-options="{ debounce: 100 }" validationKey-required="PaymentAdress1IsRequired" required maxlength="200">
  </script>

Here the vm.sadadPaymentForm.$invalid does not work but Indidual components validation works on blur on blur of input .

BUT , if I add vm to ng-form ,ie like this

    <div ng-form="vm.sadadPaymentForm" class="sadadPaymentForm"  ng-if="vm.isSadadEnabled" name="vm.sadadPaymentForm"  validate-popup-form>
     <div ng-if="vm.isSadadEnabled">
       <div class="credit-debit-card-div" ng-include="'sadadTemplate'" ng-show="vm.view_tab == 'tab7'">
       </div>
    </div>
   </div>

Here the vm.sadadPaymentForm.$invalid works but Indidual components validation fails on blur of input for eg, TypeError: Cannot read property 'onlinePaymentId' of undefined

Help me understand how can I make both individual validation and the final form validation work together.Angular Ver 1.5,cannot upgrade this now.Need a solution with 1.5.


Solution

  • Ok found out the issue , its basically a scope issue.

    replaced

    ng-if="vm.isSadadEnabled"
    

    with

    ng-show="vm.isSadadEnabled"
    

    ng-if was preventing the DOM from rendering thus killing the scope variable vm itself