扩展AngularJS ngModel指令

时间:2017-03-14 15:29:24

标签: javascript angularjs directive ngmodel

我正在尝试将$focused属性添加到我的输入字段(例如$touched$valid)。这是我目前的代码:

directive('ngModel', function () {
  return {
    restrict: 'A',
    require: 'ngModel',
    link: function ($scope, $element, $attrs, ngModelScope) {
      $element.on('focus', function () {
        ngModelScope.$focused = true
      })
      $element.on('blur', function () {
        ngModelScope.$focused = false
      })
      ngModelScope.$focused = false
    }
  }
})

..而且它不起作用。似乎$digest未触发,并且使用ngModelScope进行的更改不适用于dom。

HTML:

<form novalidate name="loginForm">
  <input type="email" placeholder="Email" name="email" ng-model="email" >
  <span ng-show="loginForm.email.$focused">Focused</span>
</form>

0 个答案:

没有答案