在控制器内部处理ng模式行为

Handling ng-pattern behaviour inside the controller

本文关键字:模式 ng 处理 控制器 内部      更新时间:2023-09-26

目前我已经设置了一个ng-show,当用户根据在ng-pattern中定义的正则表达式输入无效字符时触发;它看起来像这样:

<form name = "itemForm">
                <div class="input-group">
                    <textarea name = "listItemsArea" cols="50" rows="2" class="form-control" ng-model="summaryTabModel.itemList" ng-pattern="/^[0-9,'b's]+$/"></textarea>
                </div>
                <span ng-show="itemForm.listItemsArea.$error.pattern">Input not valid!</span>
            </form>

是否可以处理控制器内部ng-pattern上的错误?如果是,我需要传递什么给它?例如,我是否可以这样描述模式错误:

$scope.$error.pattern

谢谢

我知道一种方法,,但我认为这不是习惯用法。

检查…

function formCtrl($scope, $log) {
    $scope.$watch("itemForm.listItemsArea.$error.pattern", function(newval, oldval) {
        $log.error("Whoops", newval, oldval);
    });
}

请参阅此处http://jsbin.com/lalom/1/

var app = angular.module('app', []);
app.controller('firstCtrl', function($scope){
$scope.$watch('itemForm.listItemsArea.$error.pattern' , function(value){
$scope.isError = value;
}
              );
});