【问题标题】:ng-model inside $ionicPopup templateUrl is not working with scope$ionicPopup templateUrl 中的 ng-model 不适用于范围
【发布时间】:2016-11-18 00:33:18
【问题描述】:

这是我的控制器代码。显示一个弹出窗口并单击按钮,进行一些验证:

UZCampusWebMapApp.controller('PlanCtrl',function($scope, $ionicModal, $ionicLoading, $ionicPopup) {

    $scope.confirmCreatePOI = function(data) {
        var myPopup = $ionicPopup.show({
            templateUrl: 'templates/pois/confirmCreatePOI.html',
            title: 'Confirmar creación de POI',
            scope: $scope,
            buttons: [
                {
                    text: '<b>Save</b>',
                    onTap: function() {
                        var invalidEmail = $scope.email.length==0 || $scope.email==null || typeof($scope.email)=='undefined';
                        if ($scope.emailChecked==true && invalidEmail) {
                            $ionicLoading.show({ template: 'Email is mandatory'});
                        }
                        else {
                            data.email = $scope.email;
                            $scope.finalSubmitCreatePOI(data);
                        }
                    }
                },
                { 
                   text: 'Cancel'
                }
            ]
        });
    };
});

这是调用前一个控制器函数confirmCreatePOI的指令代码:

    UZCampusWebMapApp.directive('formCreatePointOfInterest', function($ionicLoading) {
      return {
        restrict : 'A',
        scope: true,
        controller : function($scope) {      
          $scope.submit = function(data) {
            console.log("Submit form createpoint of interest",data);
            if($scope.createPOIform.$valid) {
              $scope.confirmCreatePOI($scope.data);
            } else {
              $ionicLoading.show({ template: 'El formulario es inválido', duration: 1500})
            }
          }
        }
      }
    });

这是我的 templateUrl 代码:

<div id="confirm-create-poi-popup">
  <p> Text </p>
  <p> Text </p>
  <div class="list">
    <ion-checkbox ng-model="emailChecked">Receive notification</ion-checkbox>
    <label class="item item-input">
      <input type="email" ng-model="email">
    </label>
  </div>
</div>

所以,在用户点击'Save'按钮(onTap事件)后,我想检查是否在输入字段中输入了电子邮件。

但是当我检查它时:

var invalidEmail = $scope.email.length==0 || $scope.email==null || typeof($scope.email)=='undefined';

$scope.email.length==0 表达式返回错误,因为电子邮件未定义,所以ng-model 属性不适用于$scope,我在$scope.email 上没有得到任何值

这是为什么呢? $ionicPopup $scope 属性不起作用吗?用错了吗?

【问题讨论】:

    标签: javascript angularjs ionic-framework angular-ngmodel


    【解决方案1】:

    我用this.scope.cd_ticket解决了这个问题:

    var popup = $ionicPopup.show({
      title: 'Validar Ticket',
      scope: $scope,
      template: '<input type="tel" placeholder="Digite o código do ticket"  ng-model="cd_ticket" >',
      buttons: [{
          text: '<i class="icon ion-close"></i>',
          type: 'popup-close'
        },
        {
          text: '<i class="icon ion-checkmark"></i>',
          type: 'common-btn',
          onTap: function (e) {
            return this.scope.cd_ticket;
          }
        }
      ]
    }).then(function (res) {
      if (res) {
        $ionicPopup.alert({
          title: 'Alerta',
          template: 'Ticket: ' + $scope.cd_ticket + "<br>Resposta: " + res
        });
      }
      console.log(res);
    });
    

    【讨论】:

      【解决方案2】:

      我发现了更多可以改进的地方:)

      您的 onTap 函数应如下所示:

      onTap: function() {
      
          // CORRECTION 1 - should use this.scope instead of $scope
          var email = this.scope.email;
      
          // CORRECTION 2 - check for 'undefined; first, then 'null', then 'length'
          //  (if the first OR condition is unsatisfied, the rest won't be checked for, saving time and a possible error)
          var invalidEmail = typeof(email) == 'undefined' || email == null || email.length == 0 ;
      
          // CORRECTION 3 - Either check for existence of 'emailChecked' or initialise it to 'false' in your templateUrl
          var emailChecked = this.scope.emailChecked;
          if (emailChecked == true && invalidEmail) {
              $ionicLoading.show({ template: 'Email is mandatory' });
          } else {
              data.email = email;
              $scope.finalSubmitCreatePOI(data);
          }
      }
      

      【讨论】:

      • 仍然无法正常工作,同样的错误:TypeError: Cannot read property 'email' of undefined。我已经更新了我的问题。显示弹出窗口的函数是从指令中调用的,可能与问题本身有关。
      【解决方案3】:

      请在此处查看代码:Ionic Play - Scope Issue in Ionic Popup。

      我在控制器$scope 中的vm 对象上创建了email 和emailChecked 变量。

      $scope.vm = {
        email: '',
        emailChecked: false
      }
      

      传递封装在对象中的变量(此处为vm)是一种很好的做法,而不是将它们作为原始类型传递/使用。 vm 代表视图模型。 Understanding Scopes.

      只是一个建议,您可能会在 toast 组件中显示验证错误,这是我使用 ionicToast 而不是使用 ionicLoading 的一个。

      【讨论】:

      • 仍然无法正常工作,同样的错误:TypeError: Cannot read property 'email' of undefined。我已经更新了我的问题。显示弹出窗口的函数是从指令中调用的,可能与问题本身有关。
      • 我在您的指令中没有看到 confirmCreatePOI 方法。看起来方法调用是通过指令连接起来的。你能在控制器中直接调用并避免指令吗?
      • 问题更新,我复制了错误的代码,现在你可以看到confirmCreatePOI函数被调用了。也许我可以找到一种方法来避免该指令,但如果不是这样......问题仍然存在于范围
      • 指令中的$scope.submit如何被调用?请在调用formCreatePointOfInterest 指令的位置发布代码?
      【解决方案4】:

      我通过在 templateUrl 之前移动范围声明来解决这个问题。

      UZCampusWebMapApp.controller('PlanCtrl', function ($scope, $ionicModal, $ionicLoading, $ionicPopup) {
      
        $scope.confirmCreatePOI = function (data) {
          var myPopup = $ionicPopup.show({
            scope: $scope,
            templateUrl: 'templates/pois/confirmCreatePOI.html',
            title: 'Confirmar creación de POI',
            buttons: [{
                text: '<b>Save</b>',
                onTap: function () {
                  var invalidEmail = $scope.email.length == 0 || $scope.email == null || typeof ($scope.email) == 'undefined';
                  if ($scope.emailChecked == true && invalidEmail) {
                    $ionicLoading.show({
                      template: 'Email is mandatory'
                    });
                  } else {
                    data.email = $scope.email;
                    $scope.finalSubmitCreatePOI(data);
                  }
                }
              },
              {
                text: 'Cancel'
              }
            ]
          });
        };
      });
      

      【讨论】:

        猜你喜欢
        • 2015-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-05
        • 1970-01-01
        • 2014-08-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多