简体   繁体   English

AngularJS输入监视$ valid或$ error

[英]AngularJS input watch $valid or $error

I'm trying to $watch the $error or the $valid value of a control. 我试图$watch $error或控件的$valid值。 This is the control: 这是控件:

<form id="myForm" name="myForm"> 
  <input name="myInput" ng-model="myInputMdl" business-validation/>
</form>

business-validation is a custom directive that alters the validity of the control. business-validation是一个自定义指令,可更改控件的有效性。 I've attempted the following approaches (using either $valid or $error ) based on what I've read at AngularJS directive watch validity : 我已经根据在AngularJS指令监视有效性上阅读的内容尝试了以下方法(使用$valid$error ):

  1. This does not work since $valid does not exist on myForm.myInput . 这不起作用,因为$validmyForm.myInput上不存在。

     $scope.$watch('myForm.myInput.$valid', function (isValid) { $scope.usefulVariable = step3.CreditCardNumber.$valid; },true); 
  2. This does not work since validity.valid apparently cannot be watched. 这不起作用,因为validity.valid显然无法监视。

     $scope.$watch('myForm.myInput.validity.valid', function (isValid) { $scope.usefulVariable = step3.CreditCardNumber.$valid; },true); 
  3. This does not work since $scope.myInputMdl is not watched on every change. 这不会起作用,因为不会在每次更改时都监视$scope.myInputMdl

     $scope.$watch('$scope.myInputMdl', function (isValid) { $scope.usefulVariable = step3.CreditCardNumber.$valid; },true); 

Can't the validity be watched from a controller? 无法从控制器监视有效性吗?

EDIT 编辑

I'm not trying to write or edit business-validation directive. 我不是要编写或编辑business-validation指令。 What I'm trying is to $watch $valid or $error from form's controller. 我正在尝试的是从表单的控制器中$watch $valid$error

EDIT 2 编辑2

Controller's code is: 控制器的代码为:

app.controller('WizardBusinessActionCtrl',
   function ($scope, $http, $parse, $filter, wizardBusinessActionService, $timeout) {
     //controller code
   }
);

I don't see any reason why your first variant shouldn't work. 我看不出您的第一个变体不起作用的任何原因。

HTML: HTML:

<div ng-app="myApp">
    <div data-ng-controller="MainController">
        <form name="myForm"> 
            <input name="myInput" ng-model="myInputMdl" business-validation />
        </form>
    </div>
</div>

Controller and directive: 控制器和指令:

var myApp = angular.module('myApp', []);

myApp.controller('MainController', function ($scope) {
    $scope.$watch('myForm.myInput.$valid', function (validity) {
        console.log('valid', validity);
    });
});

myApp.directive('businessValidation', function () {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function (scope, elem, attr, ctrl) {
            function validate (input) {
                return input === 'hello';
            }

            ctrl.$parsers.unshift(function (value) {
                var valid = validate(value);
                ctrl.$setValidity('businessValidation', valid);
                return valid ? value : undefined;
            });

            ctrl.$formatters.unshift(function (value) {
                ctrl.$setValidity('businessValidation', validate(value));
                return value;
            });
        }
    };
});

$valid property value will change when myInput validity changes, and $watch callback will be fired. myInput有效性更改时, $valid属性值将更改,并且将触发$watch回调。

See example: http://jsfiddle.net/Lzgts/287/ 参见示例: http : //jsfiddle.net/Lzgts/287/

Try the code below. 试试下面的代码。 Working Plunker 工作柱塞

HTML HTML

<div ng-controller="ExampleController">
  <form name="userForm" novalidate >
    Name:
    <input type="text" name="userName" ng-model="firstName" required />
  </form>
  <div>{{firstName}}</div>
  <input type = 'button'  value = 'Submit' ng-click ='validateInput()'/>
</div>

JavaScript JavaScript的

 angular.module('getterSetterExample', [])
  .controller('ExampleController', ['$scope', function($scope) {
    $scope.validateInput = function(){
      $scope.$watch($scope.userForm.userName.$valid, function() {
       if($scope.userForm.userName.$valid){
         alert('Value is True');
       }
       else{
         alert('Value is False');
       }
      });

    }

  }]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM