How to bind to `keydown` of another element in a directive

Ben

I'm writing a directive for validation that accepts the name of an element in a form and prints error messages based on the value of the element.

This is an example (live demo link here)

  <form name="form" >
    <input type="text" name="inp" ng-model="myval" ng-minlength="3">
    <oversee watched="form.inp">
  </form>

The directive:

directive("oversee",function () {
  return {
    restrict: 'E',
    scope: {
      watched: '='
    },
    template: '<div ng-show="watched.$invalid">Error</div>',
    link: function (scope, elm) {
      console.log(scope.watched);
    }
  };
}

Here's the thing. I want to change the error message to reflect the current length of the input element it is watching. (Like how Stackoverflow does in comments)

5 more to go...

So I have to bind to the element and get it's value on every keyup

Notice - I can't use: {{myval.length}} for this because the model is not updated while the form/input is invalid.

Hope I made my question clear.

Thanks!

Narek Mamikonyan

Here is a working example, you can use $viewValue instead of model note: you also need to take care of form error messages

angular.module("angtemp", [])
.controller("first", firstCtrl)
.directive("oversee",function () {
  var LIMIT = 20;
  return {
    restrict: 'E',
    scope: {
      watched: '=',
      val: '='
    },
    template: '<div ng-show="countToType">{{countToType}} more to go...</div>',
    link: function (scope, elm) {      
      scope.$watch('val',function(newVal,oldVal){      
        if(!newVal) return;
        scope.countToType = LIMIT-newVal.length;

      });
    }
  };
});

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Bind Button.Command to another element by name

분류에서Dev

How find and remove DOM element from directive?

분류에서Dev

Binding AngularJS directive to element

분류에서Dev

directive element not showing

분류에서Dev

Angular Directive not for child element

분류에서Dev

Angular Directive not for child element

분류에서Dev

How do you bind-attr and action on the same element in handlebars?

분류에서Dev

How to change the class of an element based on the class of another

분류에서Dev

How do I center an element within a div with another element nearby?

분류에서Dev

How to pass directive parameters to directive

분류에서Dev

Bind event to wrapper element

분류에서Dev

Angular Click outside of an element in directive

분류에서Dev

How to bind an object from one window to another control in another window in wpf? (c#)

분류에서Dev

How to get `callback` from other element animation completion, not from self directive

분류에서Dev

Angular directive that requires another directive that uses an Attribute defined controller

분류에서Dev

How to change css of the element from another page using jquery?

분류에서Dev

How to get multiple XML elements that are descendants of another element

분류에서Dev

How to load directive in controller?

분류에서Dev

How to bind List<string> to gridview inside another gridview in asp.net

분류에서Dev

using jqlite to target nested directive element

분류에서Dev

Unable to properly bind data from controller to a custom directive template, Angular

분류에서Dev

How to get the closest element in a vector for every element in another vector without duplicates?

분류에서Dev

How to bring forward an element on the windows form so it appears ontop of another element

분류에서Dev

Ember bind controller property to query in another controller

분류에서Dev

knockout Bind checkboxlist checked items to another checkboxlist

분류에서Dev

Bind http request data to another controller

분류에서Dev

How to implement bind() for Lua?

분류에서Dev

How to bind data object

분류에서Dev

How to mock an event like 'keypress' or 'keydown' in an AngularJS unit test?

Related 관련 기사

  1. 1

    Bind Button.Command to another element by name

  2. 2

    How find and remove DOM element from directive?

  3. 3

    Binding AngularJS directive to element

  4. 4

    directive element not showing

  5. 5

    Angular Directive not for child element

  6. 6

    Angular Directive not for child element

  7. 7

    How do you bind-attr and action on the same element in handlebars?

  8. 8

    How to change the class of an element based on the class of another

  9. 9

    How do I center an element within a div with another element nearby?

  10. 10

    How to pass directive parameters to directive

  11. 11

    Bind event to wrapper element

  12. 12

    Angular Click outside of an element in directive

  13. 13

    How to bind an object from one window to another control in another window in wpf? (c#)

  14. 14

    How to get `callback` from other element animation completion, not from self directive

  15. 15

    Angular directive that requires another directive that uses an Attribute defined controller

  16. 16

    How to change css of the element from another page using jquery?

  17. 17

    How to get multiple XML elements that are descendants of another element

  18. 18

    How to load directive in controller?

  19. 19

    How to bind List<string> to gridview inside another gridview in asp.net

  20. 20

    using jqlite to target nested directive element

  21. 21

    Unable to properly bind data from controller to a custom directive template, Angular

  22. 22

    How to get the closest element in a vector for every element in another vector without duplicates?

  23. 23

    How to bring forward an element on the windows form so it appears ontop of another element

  24. 24

    Ember bind controller property to query in another controller

  25. 25

    knockout Bind checkboxlist checked items to another checkboxlist

  26. 26

    Bind http request data to another controller

  27. 27

    How to implement bind() for Lua?

  28. 28

    How to bind data object

  29. 29

    How to mock an event like 'keypress' or 'keydown' in an AngularJS unit test?

뜨겁다태그

보관