Angular.js - ng-change não dispara quando ng-pattern é $ inválido
Estou usando o ng-pattern para validar alguns campos do formulário e estou usando o ng-change para assistir e processar quaisquer alterações, no entanto o ng-change (ou $ scope. $ Watch) será acionado somente quando o elemento do formulário estiver no $ estado válido! Eu sou novo em angular, então não sei como resolver esse problema, apesar de suspeitar que uma nova diretiva seja o caminho a seguir.
Como faço para que ng-change seja acionado nos estados do elemento $ inválido e $ válido, com o ng-pattern ainda configurando os estados do elemento do formulário como antes?
Html:
<div ng-app="test">
<div ng-controller="controller">
<form name="form">
<input type="text" name="textbox" ng-pattern="/^[0-9]+$/" ng-change="change()" ng-model="inputtext"> Changes: {{ changes }}
</form>
<br>
Type in any amount of numbers, and changes should increment.
<br><br>
Now enter anything that isn't a number, and changes will stop incrementing. When the form is in the $invalid state, ng-change doesn't fire.
<br><br>
Now remove all characters that aren't numbers. It will increment like normal again. When the form is in the $valid state, ng-change will fire.
<br><br>
I would like ng-change to fire even when the the form is $invalid.
<br><br>
form.$valid: <font color="red">{{ form.$valid }}</font>
</div>
</div>
Javascript:
angular.module('test', []).controller('controller', function ($scope) {
$scope.changes = 0;
$scope.change = function () {
$scope.changes += 1;
};
});
Eu criei um JS Fiddle funcional que mostra o problema que estou tendo.
A propósito, essa questão angular também parece ser relevante:https://github.com/angular/angular.js/issues/1296