ng-form模板中的角度丢失范围

时间:2018-11-28 10:31:09

标签: javascript angularjs angularjs-scope

我正在尝试这个,

    <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>

然后,我的模板在另一个html文件中

  <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>

此处vm.sadadPaymentForm.$invalid不起作用,但在输入模糊时,独立组件验证也适用。

但是,如果我将vm添加到ng-form,即像这样

    <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>

这里vm.sadadPaymentForm.$invalid可以工作,但是在输入模糊时,例如 TypeError:无法读取未定义的属性'onlinePaymentId'

,单个组件验证失败

帮助我了解如何使个人验证和最终表单验证一起工作。AngularVer 1.5,现在无法升级。需要使用1.5的解决方案。

2 个答案:

答案 0 :(得分:1)

尽管name,表单sadadPaymentForm属性应具有vm.sadadPaymentForm。由于您没有正确指定表单name,因此验证失败。

name="vm.sadadPaymentForm" 

应该是

name="sadadPaymentForm" 

答案 1 :(得分:0)

确定了问题所在,基本上是范围问题。

已替换

ng-if="vm.isSadadEnabled"

使用

ng-show="vm.isSadadEnabled"

ng-if阻止了DOM的呈现,因此杀死了范围变量vm本身