为元素分配属性的自定义指令

Custom directive to assign property on element

本文关键字:自定义 指令 属性 元素 分配      更新时间:2023-09-26

我使用了这个片段,这样我就可以在模糊后看到输入上的"$touch"属性,这样我可以进行一些验证,它工作得很好,但现在我正在努力使它在不重载输入的情况下工作,我已经将其更改为:

.directive('blur', function () {
    return {
        restrict: 'E',
        require:  '?ngModel',
        replace: true,
        template: "<input />",
        link:     function postLinkFn($scope, $element, $attrs, ctrl) {
          if (!ctrl) { return; }
          ctrl.untouched = true;
          ctrl.touched   = false;
          $element.on('blur', function (){
            $scope.$apply(function () {
              ctrl.untouched = false;
              ctrl.touched   = true;
            });
          });
        }
    };
  });

希望能够使用"myForm.email.tuched",但这不起作用。我做错什么了吗?

您的代码运行良好

也许你的html代码在某种程度上是错误的
以下是我如何做到的:

<div ng-app="app">
    {{ myForm.email }}
    <form name="myForm">
        <blur type="email" ng-model="test" name="email" required></blur>
    </form>
</div>

DEMO