焦点上的$脏设定值

时间:2015-11-19 18:42:23

标签: javascript angularjs

我正在AngularJS中创建一个表单,我想要验证字段,问题是只有在输入中写入内容并且在删除它之后才会出现所需的消息,但是我希望在关注输入之后出现消息

我的代码如下

Graphics g;
g = ((Form1)this.Owner).CreateGraphics();
g.DrawRectangle(Pens.Black, 100, 100, 50, 50);

2 个答案:

答案 0 :(得分:2)

尝试以下

<span  ng-show="form.textInput.$touched && form.textInput.$error.required">Required!</span>

这会在您触摸后显示消息,并在字段无效时离开(失去焦点)。 Documentation

答案 1 :(得分:1)

是的,这是可行的。首先添加如下指令:

myApp.directive('trackFocus', ['$timeout', function($timeout) {
    return {
        restrict: 'A',
        require: 'ngModel',
        scope: true,
        link: function ($scope, element, attr, ctrl) {
            element.on("focus", function() {
                $timeout(function() {
                    ctrl.hasFocus = true;
                });
            });

            element.on("blur", function() {
                $timeout(function() {
                    ctrl.hasFocus = false;
                });
            });
        }
    }
}]);

然后使用该指令并修改您的代码:

<input type="text" name="textInput" ng-model="field.data"  class="form-control" required track-focus />

<span ng-show="form.textInput.hasFocus && form.textInput.$dirty && form.textInput.$error.required">Required!</span>
相关问题