为什么ng-show不能正常工作?

时间:2017-09-11 06:12:05

标签: javascript angularjs

我使用asp.net mvc 5和angularjs开发Web应用程序。我想知道为什么ng-show没有工作或没有显示信息。

angularjs代码:

 vm.checkEmailFormat = function () {
        var email = $(".pv-company-email").val();

        var regex = /^[\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$/i;

        vm.validEmailFormat = (regex.test(email));

        if (vm.validEmailFormat) {
            vm.pageValidated = true;
        } else {
            vm.pageValidated = false;
        }
    }

查看代码:

<div class="row">
  <div class="col-md-3 settings-padding"><span class="textdescription"><strong>Email Address</strong></span></div>
  <div class="col-md-6 settings-padding">
    <input type="text" ng-model="vm.organisation.Email" ng-blur="vm.checkIsEmailBlank()" ng-keyup="vm.checkEmailFormat()" placeholder="Email" class="form-control comp pv-company-email" style="max-width:420px;" maxlength="250" />
  </div>
  <div class="col-md-3 settings-padding"><span class="error company-error" ng-show="!vm.validEmailFormat"><span class="company-border-top"><i class="fa fa-times-circle ngdialog-register-erroricon" aria-hidden="true"></i></span> &nbsp;&nbsp;&nbsp;Invalid email format.</span>
  </div>
  @*
  <div class="col-md-3 settings-padding"><span class="error company-error" ng-show="vm.isEmailEmpty"><span class="company-border-top"><i class="fa fa-times-circle ngdialog-register-erroricon" aria-hidden="true"></i></span> &nbsp;&nbsp;&nbsp;Email is required.</span>
  </div>
  <div class="col-md-3 settings-padding"><span class="error company-error" ng-show="vm.validEmailLength"><span class="company-border-top"><i class="fa fa-times-circle ngdialog-register-erroricon" aria-hidden="true"></i></span> &nbsp;&nbsp;&nbsp;Must be 50 characters long only.</span>
  </div>*@

</div>

2 个答案:

答案 0 :(得分:3)

在视图中你必须使用<input type="text" ng-model="controllerAliseName.organisation.Email" ng-blur="controllerAliseName.checkIsEmailBlank()" ng-keyup="controllerAliseName.checkEmailFormat()" placeholder="Email" class="form-control comp pv-company-email" style="max-width:420px;" maxlength="250" /> vm.的插入,如果您发现vm.isEmailEmpty在模糊功能上为空,则需要将ng-model="controllerAliseName.organisation.Email"标记为true。你没有检查条件

答案 1 :(得分:0)

您需要在视图中应用变量。 请尝试以下方法:

if (vm.validEmailFormat) {
    $scope.$apply(function () {
        vm.pageValidated = true;
    });
}
else{
    $scope.$apply(function () {
        vm.pageValidated = false;
    })
}